Calling a loop from eventEnterFrame function AS3

94 Views Asked by At

I cannot figure out how to make a loop progress fully from the eventEnterFrame function. It does the entire loop in one frame. I am trying to make it just call the classes function and let it run thru its course. My code is trying to call a function from eventEnterFrame and then that function will call other functions and do its task.

The task is creating a random Y value, placing a movieClip there and then implementing a gravity function so the movieClip falls. The eventEnterFrame just calls the create movieClip function via a If loop so it creates multiples and they all fall at different Y locations.

I just want to clean up my eventEnterFrame function and move code out of the Main. It would not be hard to just do in the Main but I don't want it in the Main. Any help would be greatly appreciated.

private function eventEnterFrame(e:Event):void{
    if(i<10){
        i++;
    } else if(i>=10){
        spikeA.name = "spike_"+j;
        addChild(spikeA);
        j++;
        i=0;
    }

    spikeA.y+=5;
    if(spikeA.y>600){
        spikeA.y=100;
    }
}

This is how I have it just spawning one "spike" in the Main

The second issue is controlling each created "spikeA_"+j and giving each the falling class command, right now it just creates one spikeA and causes it to move downwards.

Thanks

spike code, most has been taken out from me trying a lot of ways to get it to work so it just places it since I got frustrated and did a clean slate

package  {
    import flash.events.Event;
    import flash.display.MovieClip;
    import flash.display.Stage
    import gravity;

    public class spike extends MovieClip {


        var random1:Number;


        public function spike() {
            random1 = Math.floor(Math.random()*700)+1;


            this.x = random1;
            this.y = 50;

            if(this.y>600){
                this.y=200;
            }


        }

    }
}
1

There are 1 best solutions below

4
On

First, you need to somehow instantiate a new item in order to spawn it. So somewhere you'll need to use the new keyword. If your spike is an item in your library, with export for actionscript checked in it's properties and Class name of Spike (for example), you'd probably want to do the following:

//create a container for all your spikes
private var spikeContainer:Sprite;

//create timer that ticks every 2 seconds
private var spawnTimer:Timer = new Timer(2000); 

//Then in a start game type function do the following:
public function startGame():void {
    //create and add the container to the screen
    spikeContainer = new Sprite();
    addChild(spikeContainer);

    //listen for the tick event on the timer
    spawnTimer.addEventListener(TimerEvent.TIMER, spawnSpike);
    spawnTimer.start(); //start the timer

    //listen for the enter frame event
    this.addEventListener(Event.ENTER_FRAME, enterFrame);
}

function stopGame():void {
    removeChild(spikeContainer);
    spawnTimer.stop();
    this.removeEventListener(Event.ENTER_FRAME, enterFrame);
}

private function spawnSpike(e:Event):void {
    var mySpike:spike = new spike(); //create a new spike
    spikeContainer.addChild(mySpike); //add it to the container
    mySpike.y = Math.random() * (stage.stageHeight * 0.5); //put randomly on the top half of the screen
}

private function enterFrame(e:Event):void {
    //iterate over all children of the spike container (we iterate backwards so that if you remove an item, it doesn't throw off the index)
    var i:int = spikeContainer.numChildren;
    while(i--){
        //move the spike down 5 pixels
        spikeContainer.getChildAt(i).y += 5; 

        //check to see if the spike is off stage, if it is, remove it
        if(spikeContainer.getChildAt(i).y > stage.stageHeight){
            spikeContainer.removeChild(i);
        }
    }
}

If you wanted to be more efficient, you could just recycle your spikes instead of spawning new ones and removing them.

Create all your spikes at the start (or if using FlashPro you could drop them all on the timeline inside a container movie clip)

Take out the timer and spawn function from my code above.

Then instead of removing a spike in the enterframe handler, just reset it's y position to whatever you want.

    //check to see if the spike is off stage, if it is, remove it
    if(spikeContainer.getChildAt(i).y > stage.stageHeight){
        spikeContainer.getChildAt(i).y = 100; //reset the y position
    }