Skip to main content
Known Participant
May 8, 2007
Answered

Making my enemies drop bombs in space invaders

  • May 8, 2007
  • 2 replies
  • 405 views
Hi all,

I'm in the process of learning flash just now and am completing an online tutorial on Space Invaders. Thing is, the bombs drop from a random location on the screen. I want them to drop from a random enemy (I'm not too bothered just now about making it one on the bottom row or one with no other enemies before them).

At the moment, here is the AS that makes them drop (the frequency is determined elsewhere):

eval("_root.alienBomb" + alienBombNum)._x =random(700);
eval("_root.alienBomb" + alienBombNum)._y = 300*Math.random();

An I can modify it to get a certain one to drop:

eval("_root.alienBomb" + alienBombNum)._x =_root.alienClip2_2._x;
eval("_root.alienBomb" + alienBombNum)._y = _root.alienClip2_2._y;

I can' t get it to randomly choose one - I know I have to use the random function, but don't know how to implement this.

Can anyone help?

regards,

g
This topic has been closed for replies.
Correct answer tanenbaumm
The code you're using is really old...back to Flash 5, I'd guess, which makes this rather more complicated than it need be.

Since you seem to be in AS1, I'll keep it that way. Try using:
xGridNum = Math.round(Math.random() * 7);
yGridNum = Math.round(Math.random() * 7);

var bomb = _root["alienBomb"+alienBombNum];
var alien = _root["alien"+xGridNum+"_"+yGridNum];
bomb._x = alien._x;
bomb._y = alien._y;

The above isn't the best way to do this, but it's simple and not far from what you've referenced. The sevens above assume 8 rows and 8 columns of aliens (numbered alien0_0 to alien7_7).

Hope that helps.

2 replies

tanenbaummCorrect answer
Inspiring
May 8, 2007
The code you're using is really old...back to Flash 5, I'd guess, which makes this rather more complicated than it need be.

Since you seem to be in AS1, I'll keep it that way. Try using:
xGridNum = Math.round(Math.random() * 7);
yGridNum = Math.round(Math.random() * 7);

var bomb = _root["alienBomb"+alienBombNum];
var alien = _root["alien"+xGridNum+"_"+yGridNum];
bomb._x = alien._x;
bomb._y = alien._y;

The above isn't the best way to do this, but it's simple and not far from what you've referenced. The sevens above assume 8 rows and 8 columns of aliens (numbered alien0_0 to alien7_7).

Hope that helps.
Known Participant
May 8, 2007
That's great and works perfectly - thank you.

I should have mentioned that I'm using Flash MX 2004.

Much appreciated - I'm going to be posting a couple more questions shortly, on keyboard control and the player ship firing if you feel like helping some more.

Thanks again,

g
Known Participant
May 8, 2007
Anyone?