actionscript 3 - AS3 loop for positioning enemies -


i developing game functions space invaders. far have enemies spawning in loop this:

for (var a=0; a<6; a++) {      var bd:movieclip = new bdenemy();     enemy1[a] = bd;     addchild(enemy1[a]);     enemy1[a].x = 50 + math.random() * 700;     enemy1[a].y = 50; } 

however when run game enemies spawn in parameters, overlapping of time. how can make each new enemy set x distance previous one, using array this? thanks.

optimization

before anything, should declare variable outside of loop avoid storing n variables.

answer

fill width

you may store previous x in variable , ensure there minimal gap between each enemy. if want fill screen sidth enemies, no matter how many of them.

var bd:movieclip; var previousx:uint=0; while (previousx + 150 < stage.stagewidth) {      bd = new bdenemy();     enemy1.push(bd);     addchild(bd);     bd.x = previousx + 50 + math.random() * 100;     bd.y = 50; } 

50 minimal gap between 2 enemies.

fixed amount

if want have 6 enemies, neatly dispatched on screen may introduce position span each.

var bd:movieclip; const n_enemies:uint = 6; var span:uint = stage.stagewidth / n_enemies; (var i:int=0; < n_enemies; i++) {      bd = new bdenemy();     enemy1[i] = bd;     addchild(bd);     bd.x = * span + math.random() * span;     bd.y = 50; } 

there, each enemy have random position inside span.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -