actionscript 3 - Strange issues when using addChild and hitTest with AS3 -


i having couple of problems when adding child in action script 3. building space invaders game , writing function adds asteroids stage.

my first problem previous asteroids being added each time try add new asteroid.

my second issue when add hittestoject function. throws error , doesn't when space ship hits asteroid object.

here error receive hittestobject:

typeerror: error #1034: type coercion failed: cannot convert "ast_0" flash.display.displayobject. @ spaceranger_fla::maintimeline/addastroid() @ flash.utils::timer/_timerdispatch() @ flash.utils::timer/tick()

and here code. use timer each asteroid added every 5000ms:

// add astoid var asttimer:timer = new timer(5000); asttimer.addeventlistener(timerevent.timer, addastroid); var i:number = 0; function addastroid (e:timerevent):void{     var ast = new astroid();     ast.name = "ast_"+i;     ast.y = math.random()*stage.stageheight;     ast.x = 565;     addchild(ast);     trace(i);     if(ship.hittestobject(ast.name)){         gotoandstop("2");     } = i+1; }  asttimer.start(); 

some advice, recommendations , answers appreciated :)

update

i sorted looping error. old asteroids no longer appear again! :d

many thanks,

peter stuart

per first problem, not appear i increments - it's 0.

when assign name, increment i:

ast.name = "ast_" + (i++).tostring(); 

basically, saying i = + 1;

next up, hit test against instance itself, not identity:

ship.hittestobject(ast) 

not sure how game play works, seem want 2 handlers:

  • one add new asteroid
  • one tests collisions

currently addasteroid() function adds new asteroid , tests if collides ship upon creation. asteroid never tested collision again. if similar classic asteroids game, may want push each asteroid array, , add event listener enter_frame test each asteroid collision against ship.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -