英文:
Phaser set draggable for group
问题
我有一组瓷砖,我想使它们可拖动,但我在其他答案中看到移动组或子元素是不可能的,那么除了为每个瓷砖创建不同的变量之外,有什么其他替代方法。
更新
所以我尝试创建一个容器,没有控制台错误,但我也无法拖动它。
代码:
this.grass = this.add.image(200, 100, "tileTexture", 0);
this.dirt = this.add.image(200, 116, "tileTexture", 1);
this.container = this.add.container(0, 0, [this.grass, this.dirt]);
this.container.setSize(this.grass.width, this.grass.height * 2);
this.container.setInteractive();
this.input.setDraggable(this.container);
this.input.on("drag", function (pointer, gameObject, dragX, dragY) {
gameObject.x = dragX;
gameObject.y = dragY;
});
英文:
so I have a group of tiles, which I would like to make draggable, But i've seen in other answers that moving a group or child isn't possible, so what are the alternatives to that, other that just making a different variable for each tile.
UPDATE
So i've tried making a container, and i have no console errors, but i also cannot drag it.
code
this.grass = this.add.image(200, 100, "tileTexture", 0);
this.dirt = this.add.image(200, 116, "tileTexture", 1);
this.container = this.add.container(0, 0, [ this.grass, this.dirt ]);
this.container.setSize(this.grass.width,(this.grass.height*2))
this.container.setInteractive();
this.input.setDraggable(this.container);
this.input.on("drag", function (pointer, gameObject, dragX, dragY) {
gameObject.x = dragX;
gameObject.y = dragY;
});
答案1
得分: 1
问题出在创建容器和子元素的方式上。
当您创建子元素时,您将它们定位得离容器原点“很远”。可拖动的区域仅限于从容器原点到您定义的大小。
以下是一个简短的草图,展示了这个问题:
因此,如果您希望能够点击这些瓦片并拖动它们,您需要像这样操作:
// 将容器设置在您希望瓦片出现的位置
this.container = this.add.container(200, 100, [ this.grass, this.dirt ]);
// 将瓦片相对于容器原点设置位置
this.grass = this.add.image(0, 0, "tileTexture", 0);
this.dirt = this.add.image(0, 16, "tileTexture", 1);
this.container.setSize(this.grass.width, (this.grass.height * 2));
从文档中可以了解更多信息:位置的相对性 (文档链接)。
英文:
Well the problem has to do with how you are creating the container and the children.
When you create the childeren you are positioning "far" away from the contatiner origin. And the only area that is draggable is from the contatiner origin to the size you defined.
Here a short sketch, showcasing the issue:
so if you want to be able to click the tiles and drag them you would have to something like this:
// set the container where you want the tiles to be
this.container = this.add.container(200, 100, [ this.grass, this.dirt ]);
// set the tiles relative to to container origin
this.grass = this.add.image(0, 0, "tileTexture", 0);
this.dirt = this.add.image(0, 16, "tileTexture", 1);
this.container.setSize(this.grass.width,(this.grass.height*2))
> From the documentation: ...The position of the Game Object automatically becomes relative to the position of the Container. ... for more details check (link to documentation)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论