Skip to main content
Inspiring
May 15, 2006
Question

Space Invaders

  • May 15, 2006
  • 1 reply
  • 281 views
Hi all,

I have had a little space invaders type game on the go for a while and had some free time so I thought I'd have a blast at finishing it. I have a function checking every frame whether the passed instance hits another movieclip (one of around twenty that it could hit) using onEnterFrame, a for loop to go through the different movieclips and hitTest().

The problem is this function is itself being called several times at once (its a missile that launches on pressing the space bar) so I very quickly end up with the for loop running every frame on several different instances. (I think) this causes a huge slowdown where it pretty much grinds to a halt. There are many more complex games out than mine so I assume it must be my crappy coding :o - Does anyone have any suggestions?

here is the code

stop ();

/* Control variables for use in functions */
var missileDuplicate:MovieClip; //name of the most current missile (set dynamically)
var bombDuplicate:MovieClip; // name of the most current bomb (set dynamically)
var j:Number = 3; //used to increment missile name
var k:Number = 1; //used to increment bomb name
var intervalId:Number; //counter for setInterval
var keyListener:Object = new Object ();

/*Difficulty variables eventually set automatically */
var distance:Number = 5; //speed of ship
var missileNum:Number = 200; //maximum missiles
var bombNum:Number; //number of bombs this level
var bombFreq:Number; //frequency of bombs
var bombSpeed:Number; //not used yet
var bombDiff:Number; //not used yet

bombNum = 10;
bombFreq = 3000;
bombDiff = 2;


/* setting up thew stage */
this.createEmptyMovieClip ("parent_mc", 0);

parent_mc.attachMovie ("missile_mc", "missile_mc", 2, {_x:250, _y:392});
parent_mc.missile_mc.stop ();
parent_mc.missile_mc._visible = false;

parent_mc.attachMovie ("bomb_mc", "bomb_mc", 1, {_x:250});
parent_mc.bomb_mc.stop ();
parent_mc.bomb_mc._visible = false;

missiles_txt.text = missileNum; //counts down the number of missiles left


/* Bomb control functions */
function bombsAway ()
{
var l:Number = missileNum + k;
bombDuplicate = parent_mc.bomb_mc.duplicateMovieClip ("bomb" + k + "_mc", l, {_x:Math.floor (Math.random () * (445 - 0 + 1)) + 0});
bombControl (bombDuplicate);
if (k == bombNum)
{
clearInterval (intervalId);
return;
}
k++;
}
intervalId = setInterval (this, "bombsAway", bombFreq);

function bombControl (missMov:MovieClip)
{
missMov.onEnterFrame = function ()
{
if (missMov.hitTest (base_mc))
{
this.gotoAndStop (2);
}
};
}

/*missile control*/
function hitMe (missMov:MovieClip)
{
missMov.onEnterFrame = function ()
{
if ((missMov.hitTest (blocks.blocks1)) || (missMov.hitTest (blocks.blocks2)) || (missMov.hitTest (blocks.blocks3)))
{
removeMovieClip (missMov);
}
for (var i=1; bombNum; i++){
var mov:MovieClip = MovieClip("parent_mc.bomb"+i+"_mc");

if (missMov.hitTest (mov)){
removeMovieClip(mov);
removeMovieClip(missMov);}}
};
}

/*keypress control */
keyListener.onKeyDown = function ()
{
if (Key.isDown (Key.LEFT))
{
ship_mc._x = Math.max (ship_mc._x - distance, 0);
}
else if (Key.isDown (Key.RIGHT))
{
ship_mc._x = Math.min (ship_mc._x + distance, Stage.width - 120 - ship_mc._width);
}
else if (Key.isDown (Key.SPACE))
{
if (j < missileNum + 3)
{
missileDuplicate = parent_mc.missile_mc.duplicateMovieClip ("missile" + j + "_mc", j);
missileDuplicate._x = ship_mc._x + (ship_mc._width / 2);
missiles_txt.text = missileNum + 2 - j;
j++;
hitMe (missileDuplicate);
}
}
};
Key.addListener (keyListener);


hopefully you can see the bit I'm talking about is in the hitMe function

Any help you could give would be greatly appreciated

Sam
This topic has been closed for replies.

1 reply

Craig Grummitt
Inspiring
May 16, 2006
i think you meant to write

for (var i=1; i<=bombNum; i++){

rather than

for (var i=1; bombNum; i++){

Craig