views:

26

answers:

2

hi, i'm new to AS3. how do i go about executing a custom function n number of times and then executing another function n number of times repeatedly?

eg.

function firstOne():void { }

function secondOne():void { }

i need firstOne() executed say 3 times and then secondOne() 3 times and then firstOne 3 times again and so on. i'm trying to move a movieclip 3 times to the left and then 3 times to the right continuously.

thanks

A: 

why not make a third function that has an input of how many times you want to iterate?

function thirdFunction(times_iterated:Number):void { 
   for(i=0; i<times_iterated; i++) {
     firstOne();
   }
}  

then so for the second function

corroded
i'm trying to move a movieclip 3 times to the left and then 3 times to the right continuously. i tried ur code it traces `i` 0 to 3 but the firstOne() function executes just one time
pixeltocode
can you try a trace in the first function to see if it is being called? if you don't see the first one's trace you may have a problem with scoping
corroded
A: 

i think what you have a problem with is that you are needing a time lapse if you are moving a movieclip. corrodeds answer is correct, but it will operate firstOne three times in the same frame, if you have a tween etc in there then they will just overwrite everything.

i would have a counter and function holder with a looping event, either add it to a Timer event, ENTER_FRAME event or as the onComplete event to a tween.

private var endFunc:Function = firstOne;
private var count:int = 0;

private function step(ev:Event) //CATCH ANY EVENT ETC IN HERE
{
    int ++;
    if(int>3){
        int = 0;
        if(endFunc == firstOne){ endFunc = sencondOne };
        else { endFunc = firstOne };
    }
    endFunc();
}
shortstick