views:

35

answers:

3

Im using the following code to place an object inside a container:

testParent = new MovieClip();
testParent.graphics..beginFill(0x0000FF);
testParent.graphics.drawRect(50, 50, 300, 300);
testParent.graphics.endFill();
addChild(testParent);

testChild = new MovieClip();
testChild.graphics..beginFill(0xFF0000);
testChild.graphics.drawRect(0, 0, 100, 100);
testChild.graphics.endFill();
testParent.addChild(testChild);

this gives the testParent object a margin of 50 from top and left. The testChild object should have have the same margin relative to stage. But doesnt.. The child object is at 0,0 relative to stage. Whats causing this?...

+2  A: 

You are adding testChild to testParent, not to the rectangle object that is within testParent.

Therefore the testChild will be at 0,0 relative to testParent (and not the rectangle, thats a separate object)

Or in other words, drawing a rectangle within testParent does not give it 'margins' as such.

You could try having a third object, to represent the rectangle, and then add testChild to the rectangle (and add the rectangle to the testParent)

Or, just set x and y of testChild so that its in the position that you want.

NB: I dont have AS3 handy right now to test this out, but I think this is whats going on

codeulike
A: 

Thanks, didnt know that. Now i get it.. For future info, this works:

testParent = new MovieClip();
testParent.graphics.beginFill(0x0000FF);
testParent.graphics.drawRect(0, 0, 300, 300);
testParent.graphics.endFill();
testParent.x = 50;
testParent.y = 50;
addChild(testParent);

testChild = new MovieClip();
testChild.graphics.beginFill(0xFF0000);
testChild.graphics.drawRect(0, 0, 100, 100);
testChild.graphics.endFill();
testParent.addChild(testChild);
JoaoPedro
A: 

actually, technically it should be

testParent = new MovieClip();
testParent.graphics.beginFill(0x0000FF);
testParent.graphics.drawRect(50, 50, 300, 300);
testParent.graphics.endFill();
addChild(testParent);

testChild = new MovieClip();
testChild.graphics.beginFill(0xFF0000);
testChild.graphics.drawRect(0, 0, 100, 100);
testChild.graphics.endFill();
testChild.x = 50;
testChild.y = 50;
testParent.addChild(testChild);
jonathanasdf