Question

I'm a bit new to actionscript, so please, bear with me. I'm trying to draw vectors as Sprites and add them as children under the parent 'container'. When I list the names of the children under 'container' (using trace), the Output clearly shows each instance of added Sprite. When I test the movie, I cannot see any of the Sprites. When I remove the 'test = new Sprite();' from the loop and change 'var test:Sprite;' to 'var test:Sprite = new Sprite();', I can see the Sprites. My goal is to create separate instances of the Sprite so that I may access them individually, but I can't see them. Any help would be much appreciated!

import flash.display.Sprite;
var container:Sprite = new Sprite();
container.x = 0;
container.y = 0;
addChild(container);
var test:Sprite;
var i:int = 0;

while (i < 10) {
    test = new Sprite();
    test.graphics.drawRect(0 + i*10,0 + i*10,0 + i*10,0 + i*10);
    test.graphics.beginFill(0x000000);
    test.graphics.endFill();
    test.name = "test" + i;
    container.addChild(test);
    i++;
}
for (var k:int = 0; k < container.numChildren; k++) {
    trace(container.getChildAt(k).name);
}
Was it helpful?

Solution

import flash.display.Sprite;
var container:Sprite = new Sprite();
container.x = 0;
container.y = 0;
addChild(container);
var test:Sprite;
var i:int = 0;

while (i < 10) {
    test = new Sprite();
    test.graphics.beginFill(0x000000);
    test.graphics.drawRect(0 + i*10,0 + i*10,0 + i*10,0 + i*10);
    test.name = "test" + i;
    test.x = 200;
    test.y = 100;
    container.addChild(test);
    i++;
}
for (var k:int = 0; k < container.numChildren; k++) {
    trace(container.getChildAt(k).name);
}

Do begin fill before draw rect, and I think you need to set the x and y too.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top