PDA

View Full Version : For Loop Problem


2.0 freak
09-02-2007, 12:21 AM
This for loop is going past i=2.

for(var i:Number=1;i<3;i++){
this["arrowBtn"+i].addEventListener(MouseEvent.CLICK, arrowChangeFunction);
function arrowChangeFunction(event:MouseEvent):void {
this["arrowMc"+i].gotoAndPlay(10);
}




The error is:

ReferenceError: Error #1069: Property arrowMc3 not found on Main and there is no default value.
at Main/arrowChangeFunction()



Help is greatly appreciated ;) thanks

jeff357
09-02-2007, 01:43 AM
Try i<=2

Mazoonist
09-02-2007, 01:46 AM
Hey 2.0 Freak,
See if this works for ya:

for (var i:Number=1; i<3; i++) {
getChildByName("arrowBtn" + i).addEventListener(MouseEvent.CLICK, arrowChangeFunction);
}
function arrowChangeFunction(event:MouseEvent):void {
event.target.gotoAndPlay(10);
}
Your event handler function shouldn't be included in the loop, otherwise you're declaring it multiple times. It's better form to set multiple listeners (using the for loop), but only one handler function (outside the loop). Use event.target to identify which clip was clicked on.

2.0 freak
09-02-2007, 02:48 AM
Thanks a bunch Mazoonist! I just have one small thing. How would I do the movie clip referencing correctly? I am getting an error on this script.

for (var i:Number=1; i<3; i++) {
getChildByName("arrowBtn" + i).addEventListener(MouseEvent.CLICK, arrowChangeFunction);
}
function arrowChangeFunction(event:MouseEvent):void {
getChildByName("arrowMc" + i).gotoAndStop(10);
}


the error:

1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:DisplayObject.


In case if someone was wondering the i<=2 didn't work.



Thanks!

Mazoonist
09-02-2007, 04:21 AM
You changed it! event.target identifies which clip was clicked on. In any case, you lose the value of the i variable when you leave the loop.

2.0 freak
09-03-2007, 08:23 PM
I have a button on top of an MC and I want to make it so that if you click, say the 2 button then the second MC will change colors indicating it is the one selected. But I can just make the MC with this listener. Doe! Thanks for your posts :)

panel
09-03-2007, 08:55 PM
function arrowChangeFunction(event:MouseEvent):void {
getChildByName("arrowMc" + i).gotoAndStop(10);
}


The problem is that i variable isn't defined here. One of ways would be extract i from buttons name string from arrowBtn1. evt.tagret.name will be helpfull