Developing a space shooter game
[ October 07, 2004 ] by Richard Nias aka Crashlanding
In this collection of tutorials, the author explains how to create a basic shooter game, considering all its different aspects: movement, shooting, enemies, sound, etc.


ENEMIES

If a game has no enemies, it has no fun. Enemies help give the game something to do. You can create good puzzle games without enemies, but for anything else, they are an absolute must. So let's look at what we'll be making.

The first thing you can see is that there is no collision detection. We'll be looking at that in the next chapter. So, first draw your enemies.

When you've drawn it, go back to the main timeline and give the enemy an instance name of "enemy1". Now go to the actions and add this function at the end:

var numEnemy = 3;
 
function Enemys()
{
  for (j=2; j<=numEnemy; j++)
  {
    var name = "enemy" + j;
    _root.enemy1.duplicateMovieClip(name, j);
  }
}

Enemys();

So first the variable numEnemy is set with 3. This is the number of enemies which will be on the screen at any one time. Next a for loop starts, which basically cycles through twice (depending on numEnemy) each time making a new name (enemy+j) and then duplicating enemy1. In duplicateMovieClip("this is the new name",this is the depth). Always remember that two movieclips can't have the same depth. And then the function is called. But how will our enemies move?

onClipEvent (load)
{
  function reset()
  {
    this._x = 550;
    this._y = math.random() * 300;
 
    enemySpeed = (Math.random() * 6) + 1;
  }
 
  reset();
}
 
onClipEvent (enterFrame)
{
  this._x -= enemySpeed;

  if (this._x < -10)
  {
    reset();
  }
}

Put this code on the enemy1 movie clip. When you duplicate a movie clip, it keeps its actions from the original (which is good for us). First lets look at the reset function. It sets the enemy's _x property to the far edge of the stage, and gives it a random _y between 0 and 300 using the Math.random function and multiplying it by 300 as the function in itself only makes a number between 0 and 1. It then uses the random function again to make a number between 1 and 7 which we will use as the enemy's speed, as it would be boring if they all went at the same speed. Then the function is called when the movieclip loads. Simple enough.

Now we come to on enterFrame. This is even simpler. It takes the random enemySpeed variable from the _x property making it move. Then it asks: is the enemy off the screen? If yes, the enemy goes back with the reset function.

Congratulations! Now you need to add some hitTests and you will be well on the way to making a game. If you have any problems don't hesitate to ask at richard@livescripts.net. You can download the .fla here.


     
 
 
Name: Richard Nias aka Crashlanding
Location: South-east London, UK
Age: 14
Flash experience: Had Flash for 1 1/4 years now. I learnt from the help files before finding gotoandplay and flashkit, getting me intrested in games.
Job: None
Website: http://flash.livescripts.net
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048