[SOLVED]
Hello all. I’ve been writing AS1 and 2 for a loooong time but I’m just starting to get into AS3 and it’s… challenging. Making my brain think differently when Flash is still Flash is tricky but I’m coming along.
This is my first piece of AS3 that I’m putting together and it loads an image, creates a movie clip for it to live in and then repeats the process 9 more times. The dynamically created clips are supposed to be staggered from the bottom left to the top right. However, when I run the code it only shows the final iteration in the top right. I checked this tutorial:
…and his code is almost exactly the same except for the image load. I left the image loading code out of this post since it works perfectly. There’s a listener in the image load that triggers when completed and fires the function below:
var cardNum:int = 1;
function imageLoaded(event:Event):void {
var myMC:MovieClip = new MovieClip();
myMC.name = "card" + cardNum;
addChild(myMC);
myMC.addChild(imageLoader);
myMC.width = 207;
myMC.height = 286;
myMC.x = 344 + (18.6 * (cardNum - 1));
myMC.y = 316 - (30 * (cardNum - 1));
myMC.addEventListener(MouseEvent.MOUSE_DOWN, MoveToFront, false, 0, true);
cardNum = cardNum + 1;
}
function MoveToFront(e:Event):void {
setChildIndex(e.target.name, numChildren - 1);
}
The function is properly triggered once the image load is completed and placing a trace inside the function shows that it theoretically does what it’s supposed to do, running 10 times and calculating the positions properly. The whole thing is driving me crazy and I’m sure it’s something stupid why it’s not working 100%. It always is.
As a side note, once the images are displayed they’re (theoretically) stacked on top of each other and staggered. The MoveToFront listener is designed to show the entire selected image on click by moving it to the front of the display stack. Since one image does actually show when I run my code, I can click it but when I do that I get:
TypeError: Error #1034: Type Coercion failed: cannot convert “instance356” to flash.display.DisplayObject.
at Cards_fla::MainTimeline/MoveToFront()
This is a secondary problem but still annoying.Any help would be greatly appreciated. Thanks guys!