【问题标题】:Pixijs attach element to anotherPixijs 将元素附加到另一个
【发布时间】:2015-03-25 14:38:41
【问题描述】:

我正在开发一种工具来将各种精灵添加到舞台上。当我拖动一个元素时,我想显示需要相应移动到拖动项目的边界框(一个矩形)。

为了处理拖动功能,我使用了一个名为 draggable 的库

这是我在舞台上推送的每个对象的构造函数:

function createElement(x, y, ass_id)
{
    // create our little bunny friend..
    bunny = new PIXI.Sprite(textures[ass_id]);
    bunny.scale.x = bunny.scale.y = 0.2;
    bunny.draggable({ 
        snap: true, 
        snapTolerance:0,
        grid: [ 50, 50 ],
        alpha: 0.5,
        mousedown: function(data) {
            /*var fishBounds = new PIXI.Rectangle(
                -fishBoundsPadding,
                -fishBoundsPadding,
                viewWidth + fishBoundsPadding * 2,
                viewHeight + fishBoundsPadding * 2);*/
            texture_w = (data.target.texture.width) * data.target.scale.x;
            texture_h = (data.target.texture.height) * data.target.scale.y;
        //    scale = data.target.scale.x;
            var box = new PIXI.Graphics();
            box.lineStyle(2, 0x666666);
            box.drawRect(data.target.position.x, data.target.position.y, texture_w, texture_h);
            box.scale.x = box.scale.y = scale;
            stage.addChild(box);
            data.target.boundingBox = box;
            console.log(data.target.boundingBox.position, data.target.position);
        },
        drag: function(data) {
            offset_x = data.boundingBox.position.x;//data.position;
            offset_y = data.boundingBox.position.y;
            data.boundingBox.position.x = data.position.x;// * data.scale.x;// - offset_x;
            data.boundingBox.position.y = data.position.y;// * data.scale.y;// - offset_y;

            console.log(stage.children.length , data.boundingBox.position, data.position, data);
        },
        mouseup: function(data) {
            console.log("drop");
            stage.removeChild(data.target.boundingBox);
        }
    });

    // move the sprite to its designated position
    bunny.position.x = x;
    bunny.position.y = y;

    elements.push(bunny);

    // add it to the stage
    stage.addChild(elements[elements.length-1]);
}

现在,这就像一个魅力:当我单击元素时,会在正确的位置创建一个边界框,问题是当我开始围绕边界框拖动它时,它会远离项目。我认为造成这种情况的原因可能是因为一个项目被缩放,而另一个不是,但由于我是 pixi 的菜鸟,我真的发现自己被它困住了。

【问题讨论】:

    标签: javascript pixi.js


    【解决方案1】:

    好的,我发现你可以通过 addChild 轻松方便地将一个对象附加到另一个对象上,所以它是这样的:

    function createElement(x, y, ass_id)
    {
        // create our little bunny friend..
        bunny = new PIXI.Sprite(textures[ass_id]);
        bunny.scale.x = bunny.scale.y = 0.2;
        bunny.draggable({ 
            snap: true, 
            snapTolerance:0,
            grid: [ 50, 50 ],
            alpha: 0.5,
            mousedown: function(data) {
                texture_w = (data.target.texture.width);
                texture_h = (data.target.texture.height);
    
                var box = new PIXI.Graphics();
                box.lineStyle(5, 0x666666);
                box.drawRect(0, 0, texture_w, texture_h);
                data.target.type = "element";
                data.target.addChild(box);
            },
            drag: function(data) {
    
            },
            mouseup: function(data) {
                console.log("drop");
                for (var i = stage.children.length - 1; i >= 0; i--) {
                    if((stage.children[i].type) && (stage.children[i].type == "element"))
                        for (var j = stage.children[i].length - 1; i >= 0; i--) {
                            console.log('remove boundingBox child here when needed');
                        }
                };
            }
        });
    
        // move the sprite to its designated position
        bunny.position.x = x;
        bunny.position.y = y;
    
        elements.push(bunny);
    
        // add it to the stage
        stage.addChild(elements[elements.length-1]);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2020-10-29
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多