javascript - pixi js : drag and drop circle

标签 javascript drag-and-drop pixi.js

我开始使用 pixijs 创建简单的 js 游戏.我需要图形圈的拖放功能,但找不到这方面的信息。我只看到 this example与 Sprite 。

最佳答案

对于图形和 Sprite ,拖放操作的方式相同。

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 );
}

关于javascript - pixi js : drag and drop circle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36678727/

相关文章:

javascript - 需要小代码的帮助

javascript - 即使功能被覆盖层隐藏,鼠标光标也会在功能上变化

拖动时Android ImageView消失了

javascript - 让 PIXI JS 从动态设置的高度和宽度进行缩放

javascript - 为什么我无法使用组合构造函数/原型(prototype)模式访问此实例变量?

javascript - CSS3 Translate 3d 转换与 JavaScript 拖动

apache-flex - 如何限制 Canvas 中的拖放区域

javascript - 使用模板时如何在 div 上添加可拖动功能

javascript - 如何在javascript上将对象属性绑定(bind)到this关键字?

javascript - 使用 PIXI JS 更改弯曲文本中的字体大小