arrays - Objects stuck at top of stage and won't fall down -
i using math.random randomly drop objects top of stage. had working 1 object. wanted increase number 6 objects, added following code: "stuck" , 6 objects @ top of stage. doing wrong here? appreciate help.
private function bombinit(): void { roachbombarray = new array(); (var i:uint =0; < numbombs; i++) { roachbomb= new roachbomb(); roachbomb.x = math.random() * stage.stagewidth; roachbomb.vy = math.random() * 2 -1; roachbomb.y = -10; addchild(roachbomb); roachbombarray.push(roachbomb); } addeventlistener(event.enter_frame, onentry); } private function onentry(event:event):void { (var i:uint = 0; i< numbombs; i++) { var roachbomb = roachbombarray[i]; vy += ay; roachbombarray[i] += vy; if (roachbombarray[i] > 620) { removechild(roachbombarray[i]); removeeventlistener(event.enter_frame, onentry);
you're removing enterframe listener when first bomb goes off bottom, @ point you're no longer listening enter_frame events , updating of bombs.
you don't want remove listener until you're done animating bombs.
update: how expect things look, incorperating ethan's observation ought use local roachbomb declare...
public class bombdropper extends sprite { private static const gravity:int = 1; // set gravity want in pixels/frame^2 private static const bottom_of_screen:int = 620; private var numbombs:int = 6; private var roachbombarray:array; // ... constructor , other class stuff here private function bombinit(): void { roachbombarray = new array(); (var i:int =0; < numbombs; ++i) { var roachbomb:roachbomb = new roachbomb(); roachbomb.x = math.random() * stage.stagewidth; roachbomb.vy = math.random() * 2 -1; roachbomb.y = -10; this.addchild(roachbomb); roachbombarray.push(roachbomb); } this.addeventlistener(event.enter_frame, onentry); } private function onentry(event:event):void { each ( var roachbomb:roachbomb in roachbombarray) { roachbomb.vy += gravity; roachbomb.y += vy; if (roachbomb.y > bottom_of_screen) { this.removechild(roachbomb); roachbombarray.splice(roachbombarray.indexof(roachbomb),1); if (roachbombarray.length == 0) { this.removeeventlistener(event.enter_frame, onentry); } } } } }
Comments
Post a Comment