【问题标题】:pixi js: drag and drop circlepixi js:拖放圆圈
【发布时间】:2016-08-09 06:58:36
【问题描述】:

我开始使用pixijs 创建简单的 js 游戏。我需要图形圈的拖放功能,但找不到相关信息。我只看到带有精灵的this example

【问题讨论】:

    标签: javascript drag-and-drop pixi.js


    【解决方案1】:

    拖放对图形和精灵的工作方式相同。

    var renderer = PIXI.autoDetectRenderer(800, 600, { antialias: true });
    document.body.appendChild(renderer.view);
    
    var stage = new PIXI.Container();    
    stage.interactive = true;
    
    var graphics = new PIXI.Graphics();
    graphics.interactive = true;
    graphics.lineStyle(0);
    graphics.beginFill(0xFFFF0B, 0.5);
    graphics.drawCircle(0, 0, 60);
    graphics.endFill();
    graphics.x = 100;
    graphics.y = 100;
    stage.addChild(graphics);    
    
    // setup events
    graphics
        .on('mousedown', onDragStart)
        .on('touchstart', onDragStart)
        .on('mouseup', onDragEnd)
        .on('mouseupoutside', onDragEnd)
        .on('touchend', onDragEnd)
        .on('touchendoutside', onDragEnd)
        .on('mousemove', onDragMove)
        .on('touchmove', onDragMove);
    
    function onDragStart(event)
    {
        // store a reference to the data
        // the reason for this is because of multitouch
        // we want to track the movement of this particular touch
        this.data = event.data;
        this.alpha = 0.5;
        this.dragging = true;
    }
    
    function onDragEnd()
    {
        this.alpha = 1;
    
        this.dragging = false;
    
        // set the interaction data to null
        this.data = null;
    }
    
    function onDragMove()
    {
        if (this.dragging)
        {
            var newPosition = this.data.getLocalPosition(this.parent);
            this.position.x = newPosition.x;
            this.position.y = newPosition.y;
        }
    }
    
    // run the render loop
    animate();
    
    function animate() {
    
        renderer.render(stage);
        requestAnimationFrame( animate );
    }
    

    【讨论】:

    • 感谢您的回答。我实施了非常相似的方法。关于放置区:如果我想知道一个圆圈何时高于另一个圆圈。看起来我需要使用 Circle 类的 contains 方法。对吗?
    • 你为什么不链接你的来源? scottmcdonnell.github.io/pixi-examples/…
    【解决方案2】:

    您还需要为该圆圈提供hitArea 才能将鼠标事件附加到它。 Read This Answer by GoodBoyDigital.

    【讨论】:

    • 这个作为评论比较好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多