actionscript 3 - Calling a loop from eventEnterFrame function AS3 -


i cannot figure out how make loop progress evententerframe function. entire loop in 1 frame. trying make call classes function , let run thru course. code trying call function evententerframe , function call other functions , task.

the task creating random y value, placing movieclip there , implementing gravity function movieclip falls. evententerframe calls create movieclip function via if loop creates multiples , fall @ different y locations.

i want clean evententerframe function , move code out of main. not hard in main don't want in main. 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 how have spawning 1 "spike" in main

the second issue controlling each created "spikea_"+j , giving each falling class command, right creates 1 spikea , causes move downwards.

thanks

spike code, has been taken out me trying lot of ways work places since got frustrated , did 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;             }           }      } } 

first, need somehow instantiate new item in order spawn it. somewhere you'll need use new keyword. if spike item in library, export actionscript checked in it's properties , class name of spike (for example), you'd want following:

//create container spikes private var spikecontainer:sprite;  //create timer ticks every 2 seconds private var spawntimer:timer = new timer(2000);   //then in start game type function following: public function startgame():void {     //create , add container screen     spikecontainer = new sprite();     addchild(spikecontainer);      //listen tick event on timer     spawntimer.addeventlistener(timerevent.timer, spawnspike);     spawntimer.start(); //start timer      //listen 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 new spike     spikecontainer.addchild(myspike); //add container     myspike.y = math.random() * (stage.stageheight * 0.5); //put randomly on top half of screen }  private function enterframe(e:event):void {     //iterate on children of spike container (we iterate backwards if remove item, doesn't throw off index)     var i:int = spikecontainer.numchildren;     while(i--){         //move spike down 5 pixels         spikecontainer.getchildat(i).y += 5;           //check see if spike off stage, if is, remove         if(spikecontainer.getchildat(i).y > stage.stageheight){             spikecontainer.removechild(i);         }     } } 

if wanted more efficient, recycle spikes instead of spawning new ones , removing them.

create spikes @ start (or if using flashpro drop them on timeline inside container movie clip)

take out timer , spawn function code above.

then instead of removing spike in enterframe handler, reset it's y position whatever want.

    //check see if spike off stage, if is, remove     if(spikecontainer.getchildat(i).y > stage.stageheight){         spikecontainer.getchildat(i).y = 100; //reset y position     } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -