Coordinates of a child sprite in AS3

advertisements

I have a larger project, but I can narrow my problem down to this example:

public function Main()
    {
        var myContainer:Sprite = new Sprite;
        myContainer.graphics.beginFill(0xFFFFFF);
        myContainer.graphics.lineStyle(1);
        myContainer.graphics.drawRect(10, 10, 300, 300);
        myContainer.graphics.endFill();
        addChild(myContainer);
        trace('Container coords: ' + myContainer.x + ' ' + myContainer.y);
        trace('Instance coords: ' + getChildAt(numChildren - 1).x + ' ' + getChildAt(numChildren - 1).y);
        var spr:Sprite = new Sprite;
        spr.graphics.beginFill(0xFF0000);
        spr.graphics.lineStyle(1);
        spr.graphics.drawCircle(30, 30, 10);
        spr.graphics.endFill();
        myContainer.addChild(spr);
        trace('Child coords: ' + spr.x + ' ' + spr.y);
        trace('Child instance coords: ' + myContainer.getChildAt(myContainer.numChildren - 1).x + ' ' + myContainer.getChildAt(myContainer.numChildren - 1).y);
    }

The output is this:

Container coords: 0 0
Instance coords: 0 0
Child coords: 0 0
Child instance coords: 0 0

Can someone explain how to get the actual coordinates of a child sprite within a sprite (either local or global, I know you can convert between the two)?


your myContainer not set x,y. only has been set graphics draw coordinates (10,10). spr versa.

see a following code. this code some different, but visual output same. Is set to 10 the parent's coordinates. draw coordinates set (0,0). so myContainer x,y is 10 and i think, you are slightly mistaken. getChildAt(numChildren-1) is myContainer. so this coordinats also same. myContainer not have any children. graphics is not a child concept. Additionally, child coordinates is relative from parent.

var myContainer:Sprite = new Sprite;
myContainer.x = 10;
myContainer.y = 10;
myContainer.graphics.beginFill(0xFFFFFF);
myContainer.graphics.lineStyle(1);
myContainer.graphics.drawRect(0, 0, 300, 300);
myContainer.graphics.endFill();
addChild(myContainer);

//myContainer.x = 10, myContainer.y=10
//getChildAt(numChildren - 1).x = 10, getChildAt(numChildren - 1).y = 10

your code as follows:

my code as follows: