Wednesday, June 13, 2012

[Action Script] setChildIndex causes infinite loop and new movieclips! June,2012

Before I go on and break down my code for you guys, let me give you the situation.
I want to have 2 movieclips (BattleAnimation1, BattleAnimation2), each with 1 movieclip inside them (Character). The Character movieclip has 100 frames, and each of those frames is a unique character design. The BattleAnimation movieclips animate attacks, and have drawings of projectiles, such as laser beams.

When BattleAnimation1 attacks, it should be on top (depth-wise) of BattleAnimation2.
When BattleAnimation2 attacks, it should be on top (depth-wise) of BattleAnimation1.
This is crucial. otherwise the laser beams and whatnot will go under the enemy instead of appearing to hit them!

Alright, I'm no idiot. My code compiles just fine, and from a logical stand point it should work. However, when I add the lines of code:

this.setChildIndex(BattleAnimation1, 0);
this.setChildIndex(BattleAnimation2, this.numChildren-1);

It does 3 things: First, it makes it so BattleAnimation2 attacks repeatedly (does an attack animation, does damage, the health bar animates, then it starts over). Second, BattleAnimtion2's Character is reset to frame 1 (meaning it is the WRONG character attacking). Third, BattleAnimation2's (old) Character is still there! So I have a NEW Character doing the attacks, while the correct Character just stands still forever.

I have no Idea why setChildIndex will have these strange effects. Even weirder, the entire game works fine WITHOUT those two lines of code, meaning that BattleAnimation2 will attack once, stop, then BattleAnimation1 will attack once, stop, etc...

I put the BattleAnimation movieclips on the stage on Frame 1, and they never get changed other than by code. Inside both BattleAnimation movieclips, I have a line of code that dispatches an Event that I created named AnimationEvent so I can know when the animations start and stop. Not sure if that is relevant at all.

P.S. If I cannot resolve this problem, I have thought of two work-arounds, but they would require some back tracking and who knows if I'll run into a new problem...
setChildIndex causes infinite loop and new movieclips!

Related Post



0 comments: