CreateJS - 用父级更新子坐标

时间:2013-08-16 17:18:42

标签: javascript createjs

我想更改父容器中2个子容器的坐标。最初,我循环遍历父容器的子容并单独更改它们x,y ...但是更改父容器的x,y ...更有意义...

问题是......我需要获得每个孩子的个体改变坐标......但是改变父容器的坐标似乎不会改变孩子们与舞台相关的坐标......

问题是......当我改变父母的x,y时,我怎样才能改变孩子的x,y

由于


所以如果我这样移动儿童容器:

function NPCMove() {
if (pointA) {
    if (ContainerOfAnimals.x < 400) {
        ContainerOfAnimals.x +=2;
    }
    else {
        pointA = false;
        pointB = true;
    }
}
else if (pointB) {
    if (ContainerOfAnimals.x > 100) {
        ContainerOfAnimals.x-=2;
    }
    else {
        pointB = false;
        pointA = true;                  
    }
}
}

我可以使用localToGlobal检查玩家与父容器中每个孩子的距离吗? (NPC_Array包含父容器)

for (var i = 0; i < NPC_Array.length; i++) {
//get children containers of each big Container
for (var j = 0; j < NPC_Array[i].children.length; j++) {
    //need child.x's global location now...
    var pt =  NPC_Array[i].localToGlobal(NPC_Array[i].children[j].x, NPC_Array[i].children[j].y);

    var distX = Math.abs(players_Array[0].x - pt.x);
    var distY = Math.abs(players_Array[0].y - pt.y);

    if (distX < 50 && distY < 50) {
        //Player is near child...

1 个答案:

答案 0 :(得分:3)

你会做一个localToGlobal:

var stage = new createjs.Stage("test");

var p = new createjs.Container();
p.x = 200;
p.y = 200;

var c1 = new createjs.Shape();
c1.graphics.beginFill("#FF0000");
c1.graphics.drawRect(0, 0, 100, 100);
c1.graphics.endFill();

var c2 = new createjs.Shape();
c2.graphics.beginFill("#00FF00");
c2.graphics.drawRect(0, 0, 100, 100);
c2.graphics.endFill();
c2.x = 100;

p.addChild(c1);
p.addChild(c2);

stage.addChild(p);

stage.update();

var pt = p.localToGlobal(c2.x, c2.y);
alert("Stage x of c2: " + pt.x);

See it in action