PDA

View Full Version : addChild help to create multiple instances of an MC on the stage


jmeade11
06-28-2009, 04:08 PM
Hi -- I'm having a really difficult time with this and any help would be great. I'd like to create a chain of objects (either movie clips or sprites) that move left to right across the stage continuously. I know I can create two movie clips that contain a series of several 'chain links' and are each a little wider than the stage width. Then I can move both clips across the stage and when the first movie clip is completely off stage reset its x position back to 0. Likewise, when the second clip is completely off the stage on the right, reset it's position off the stage to the left.

This works and gives me the basic effect that I want by creating this uninterrupted moving chain, but it doesn't seem like its the most efficient way to achieve this. What I thought would be more efficient would be to create each link in the chain separately from an individual mc in my library by programmatically adding new instances to the stage.

So, I created a single chain link mc and set its export linkage to 'Link' and left the base class to flash.display.MovieClip. Next, I placed the following snippet of the code on the first frame of my movie:

var xPos:int = 0;
var yPos:int = 100;
var chainTest:Link;

for(var i:int = 0; i < 1000; i++){
chainTest = new Link();
chainTest.x = xPos;
chainTest.y = yPos;
addChild(chainTest);

xPos += 100;
}

When I test the movie (CTRL+Enter) in Flash it works. The chain is created on the screen from the single Link mc in my Library, which would seem to me to be a whole lot more memory friendly. BUT, when I publish the movie and then test the SWF in the player, I get nothing!

What am I doing wrong?

--Helpless noob

Wispa
07-03-2009, 03:46 PM
You could try storing all of the chain links within an array like so:

var xPos:int = 0;
var yPos:int = 100;
var chainTest:Link;
var arrChain:Array = new Array();

for(var i:int = 0; i < 1000; i++){
chainTest = new Link();
chainTest.x = xPos;
chainTest.y = yPos;
addChild(chainTest);
arrChain.push(chainTest);

xPos += 100;
}

By doing it this way, you can reference each chain link as it's array item:


arrChain[0].y = 200;


The code isn't tested but the theory's there :)

Hope this helps.