Stage.addchild将对象带到前面

时间:2015-10-14 11:58:37

标签: javascript drag-and-drop html5-canvas createjs

我有一个包含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;
      }
    }
  }

1 个答案:

答案 0 :(得分:1)

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

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

应该在鼠标事件功能中执行操作。

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