actionscript 3 - Trying to make a game where i dodge meteorites falling from the sky. as3 -
im trying make game meteorites falling sky... far have got 1 falling , dissapearing isnt looping. how make multiple meteorites come down @ different locations , keep looping code far
var randomx:number = math.random() * 400; test_mc.x = randomx; test_mc.y = 0; var speed:number = 10; test_mc.addeventlistener(event.enter_frame, movedown); function movedown(e:event):void { e.target.y += speed; if(e.target.y >= 500 ) { test_mc.removeeventlistener(event.enter_frame, movedown); } }
instead of having 1 test_mc object, define array , counter variable track when new meteorite should added:
var meteorites:array = new array(); var counter:int = 0;
instead of adding event listener single meteorite add event listener stage , have trigger game loop:
stage.addeventlistener(event.enter_frame, gameloop); function gameloop(e:event):void { counter ++; if (counter>=10) { // add new meteorite every 10 frames counter = 0; meteorites.push(new meteorite()); addchild(meteorites[meteorites.length-1]); // add code here position new meteorite (meteorites[meteorites.length-1]) randomly in x direction } if (meteorites.length>0) { (var loop:int=meteorites.length-1;loop>=0;loop--) { meteorites[loop].y += speed; if (meteorites[loop].y>500) { removechild(meteorites[loop]); meteorites.splice(loop, 1) // removes meteorite @ index [loop] array } } } }
for work have enable meteorite movieclip actionscript (in library properties/advanced panel) , give class name of meteorite.
edit i've added in 'addchild' , 'removechild' calls necessary display meteorites. not need meteorites positioned on stage in flash. code add them in-game you.
Comments
Post a Comment