actionscript 3 - as3 array of objects - movement with constant speed -


ok, have experience as3 , of basics. problem has been stumping me long. tried workaround based on know as3. somehow either error message or doesn't @ all. here code i'm trying solve.

    var zombiecount:array = new array();      var helltime:timer = new timer(1500);     helltime.addeventlistener(timerevent.timer, spawnzombies)        helltime.start();     function spawnzombies(happened:timerevent){         var zombie1:zombie = new zombie();         zombiecount.push(zombie1);         stage.addchild(zombiecount[zombiecount.length - 1]);         zombie1.x = 135 + (330*math.random())         zombie1.y = -29           stage.addeventlistener(event.enter_frame, move_zombie)          function move_zombie(happened:event){             for(var i:int; < zombiecount.length; i++){                 zombiecount[i].y = zombiecount[i].y + 1;                 if(zombiecount[i].hittestobject(border)){                     stage.removechild(zombiecount[i]);                     zombiecount.shift();                     trace(zombiecount.length);                 }              }            }     } 

while may not inclusive of wrong, here @ least few of issues see.

  1. inline function issue:

    inside timer tick handler (spawnzombies), create inline function called move_zombie , add enter frame handler calls function.

    the issue here, every tick of timer, create whole new copy of function, , add enter frame handler. create huge problems after few timer ticks.

    break move_zombie function out of spawn function:

    eg:

    helltime.addeventlistener(timerevent.timer, spawnzombies) helltime.start();  stage.addeventlistener(event.enter_frame, move_zombie);  function move_zombie(......  function spawnzombies(..... 
  2. iteration issue:

    in loop:

        for(var i:int; < zombiecount.length; i++){         zombiecount[i].y = zombiecount[i].y + 1;         if(zombiecount[i].hittestobject(border)){             stage.removechild(zombiecount[i]);             zombiecount.shift();             trace(zombiecount.length);         }     } 

    you not initializing i value. while default 0, it's still idea readability initialize it.

    so iterating forward 0 end of array. however, if hit test succeeds, use shift method of array. removes first item of array (irrespective of value i @ time). remove wrong item, plus mess zombiecount[i] refers (because amount of items has changed after doing shift, next iteration zombiecount[i] reference same item previous iteration).

    instead of you're doing, use splice method remove, , iterate backwards index doesn't out of whack.

    for(var i:int=zombiecount.length-1;i >=0;i--){     zombiecount[i].y += 1; //move down 1 pixel     if(zombiecount[i].hittestobject(border)){         stage.removechild(zombiecount[i]);         zombiecount.splice(i,1); //remove item @ current index (do instead of shift)         trace(zombiecount.length);     } } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -