Skip to main content
Inspiring
September 16, 2015
Answered

For Loop Isn't Running Correctly

  • September 16, 2015
  • 11 replies
  • 869 views

I have some code for an RPG battle system where, when the menu's attack button is pressed, the code goes through all the stages children, determines which are linked to the class Enemy, and then adds an event listener to each one.

function targetParse():void{

  for(var i:uint = 0; i < stage.numChildren; i++){

       var enemy:Object = stage.getChildAt(i);

       trace('enemy identified')

       if(enemy is Enemy){

       trace('its an enemy')

       enemy.addEventListener(MouseEvent.MOUSE_OVER, arrowControl);

       trace('listener added')

       }

       else{}

  }

}

All these trace statements are for debugging. It gets to 'enemy identified' but doesn't seem to be running the if statement.

I think it's probably my syntax.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer mcdermitt

It spits out 4, two enemies, 1 menu, and one arrow.

It also spits out 'keep parsing' which is one of the trace statements under the else statement in targetParse()

So it's detecting two things, an enemy and the menu or the arrow, and doing the right thing about them. It seems like once the code runs once, it breaks the for loop.


I got it now.

function targetParse():void{

  menu.alpha = 0.5

  menu.attackBtn.removeEventListener(MouseEvent.CLICK, playerTurn);

  for(var i:uint = 0; i < numChildren; i++){

  var enemy:Object = getChildAt(i);

  if(enemy is Enemy){

  enemy.addEventListener(MouseEvent.MOUSE_OVER, arrowControl);

  trace('enemy' + i);

       }

  }

}

The enemy variable had to be redefined every time, it used to be above the for loop. It works like a charm now.

11 replies

Inspiring
September 16, 2015

Get rid of stage. in both places. So code is now:

function targetParse():void{

  for(var i:uint = 0; i < numChildren; i++){

       var enemy:Object = getChildAt(i);

       trace('enemy identified')

       if(enemy is Enemy){

       trace('its an enemy')

       enemy.addEventListener(MouseEvent.MOUSE_OVER, arrowControl);

       trace('listener added')

       }

       else{}

  }

}

mcdermittAuthor
Inspiring
September 16, 2015

Ok, I did that.

Now it puts the event listener on one of the enemies on screen (there are 2) and doesn't do anything for the other. I need it to add to every Enemy on screen.

If there's another way to do this that would be great.

Inspiring
September 16, 2015

The code will work fine if all your enemies are placed in the UI and they are all in the same container. If they are not, and you're placing with code, then you will need to getChildAt and numChildren on the proper container. Put in more traces and see how many numChildren are being reported.

Also, if you try what you had before, just for learning, and do stage.numChildren you will see there is just 1 - the MainTimeline object.

If I stick 10 instanced of a clip onto the stage in the Flash UI and run that function, all of them get assigned properly.