gpt4 book ai didi

javascript - Stage.addchild 将对象置于前面

转载 作者:行者123 更新时间:2023-12-03 08:38:31 24 4
gpt4 key购买 nike

我有一个排序游戏,其中有 10 个元素可以按正确的顺序拖放。

当我开始拖动一个对象时,我希望它出现在所有其他对象的前面。由于 createjs 没有 zindex 属性,我被告知要使用 stage.adchild(sequenceNumbers); 将项目重新添加到舞台上。

如何正确地将其添加到 mousedown 或 pressmove 函数中?

for (var a = 0; a < gameData.Terms.length; a++) {
rect = new createjs.Shape();
rect.graphics.beginFill("blue").drawRoundRect(0, 0, 350, 30, 8);
rect.name = a;

var name = new createjs.Text(gameData.Terms[a].Definition, "14pt arial bold", "white");
name.id = gameData.Terms[a].Name;
name.textAlign = "left";
name.y = rect.y + 2;
name.x = rect.x + 4;

var sequenceNumbers = new createjs.Container();

sequenceNumbers.landingSpot = landingSpots[a];
landingSpots[a].sequenceNumber = sequenceNumbers;

sequenceNumbers.addChild(rect, name);
stage.addChild(sequenceNumbers);
sequenceNumbers.x = 300;
sequenceNumbers.y = xOffset;
xOffset += 40;

var startPositionX;
var startPostitionY;
sequenceNumbers.on('mousedown', function (e) {
stage.adchild(sequenceNumbers);

var posX = e.stageX;
var posY = e.stageY;
startPositionX = e.stageX;
startPositionY = e.stageY;
this.offset = { x: this.x - posX, y: this.y - posY };
});

sequenceNumbers.on("pressmove", function (evt) {
evt.currentTarget.x = evt.stageX;
evt.currentTarget.y = evt.stageY;
var posX = evt.stageX;
var posY = evt.stageY;
this.x = posX + this.offset.x;
this.y = posY + this.offset.y;

stage.update();
});

sequenceNumbers.on("pressup", function (evt) {
var stuffUnderMouse = landingSpotContainer.getObjectsUnderPoint(evt.rawX, evt.rawY, 0);
var oldSpot = evt.currentTarget.landingSpot;
var spotToMoveTo = null;
for (var i = 0; i < stuffUnderMouse.length; ++i) {
if (typeof stuffUnderMouse[i].landingSpot !== 'undefined' && stuffUnderMouse[i].landingSpot != oldSpot) {
spotToMoveTo = stuffUnderMouse[i].landingSpot;
break;
}
}
}

最佳答案

只需再次将子容器添加到其父容器中即可:

evt.currentTarget.parent.addChild(evt.currentTarget);

应该在鼠标事件函数中完成这个技巧。

请注意,该函数称为 addChild,而不是 adchild。

关于javascript - Stage.addchild 将对象置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33124671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com