The Pirate
05-22-2007, 03:54 PM
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/TitleImage.png
You guys asked for it, and here it is. The Pirate's big tutorial on making a space game in Flash. In part 1 of the tutorial I'm going to walk you through the absolute basics. By the end you will have a very simple little space game ready to be expanded on.
This part of the tutorial will be done in a few main sections:
Getting started
Constructing your spaceship
Equipping your weapon
Spawning enemy spaceships
Collision Detection
Finishing touches
Before we start, I should make something clear. This tutorial assumes that you are already familiar, and reasonably skilled with the Flash program itself. You should know things like, where the actionscript tab is, where the properties tab is, how to set an instance name, how to create and work with movie clips, and so on. I'm not going to explain precisely, step by step, which buttons to press and what to click on. You need to know what you're doing.
If you meet the above criteria, you're ready to start the tutorial.
Step 1 - Getting Started
We're going to start by making a major decision that will affect how you go about creating everything else in the game. Do we want our game to be a top scrolling, or a side scrolling game?
For my example game, I am going to be making a top scroller. the things you learn in this tutorial will allow you to create either one, you just need to use some common sense and change the code around.
Just remember this:
In Flash, and basically every program you will ever come across, positions are measured in X and Y values. The X value is the horizontal position, the Y value is the vertical postition. I'm going to assume you already have SOME knowledge of how coordinates work.
If for example, I am making an object move upwards, I may type some code that deals with the Y-value. If you are making a side scrolling shooter, you will probably want that same object to be travelling sideways in your game, so you use the same code that I type BUT use the X-value instead. Just use some common sense and play with the code so it works with what you're trying to do.
For reference, an X value of 0 is the very left edge of your stage, and a Y value of 0 is the very top of the stage.
Alright, so since I'm making a top scroller, I'm probably going to want my stage to be tall and narrow. You can make your game any size you want, in general, you want a side scroller to be a short wide rectangle, and the opposite for a top scroller.
I'm going to use a width of 300, and a height of 400.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/001.png
While I'm here, I also set the background to black, so it will look more like space. Obviously you don't HAVE to set your game in space. Draw an elaborate scrolling background if you want, be creative.
Next, we're going to set the frame rate. I use 30fps for games to make them run smoothly, you can use anything you want.
Now that the stage is set up, we're going to set two Global Variables. Global variable is the name I use to refer to variables on the main timeline, instead of in an object. You should have a single blank keyframe on your timeline, click on it, then open the Actionscript tab. Enter this code:
stagex = *the width you set for your game*;
stagey = *the height you set for your game*;
NOTE: I use "stagex" and "stagey" for simplicity, but you can name your variables anything you want. The name of the variable doesn't actually matter, since we decide what the variable does when we actually write code.
These variables will be infinitely useful if you want to change your game later on. For example, lets say the height of my game was 300. I have some code that makes enemies dissapear when they go past the 300 mark, to make sure they don't keep going when they go off the screen. Later on, I decide the game screen is too short, and want to make the game 500 pixels high instead.
That means I have to go to each enemy and change it's code to use 500 instead. But, if I'm using global variables in my code, it's as easy as clicking on the timeline, and changing the global.
Now that your stage is set up, and it's dimensions have been noted in your global variables, you're ready to start making the game.
Step 2 - Constructing Your Spaceship
Now that we have a rectangle of pixelated space to work with, we need a spaceship so we can fly around in it. It's time to use your insane Flash drawing skills. Draw the most badass space cruiser you can imagine.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/002.png
Here's mine, I opted for the sleek triangular look and painted it red to make it go faster. Remember to keep your spaceship reasonably small. Once you've drawn it, you'll want to convert it into a Movie Clip. All of the objects in our space game will be movie clips, that way we can add code to them.
And that's exactly what it's time to do. We've got to write some code to allow us to fly our new spaceship.
Select your spaceship and open the actions tab. Enter this code:
onClipEvent(load){
speed = 5;
}
"onClipEvent(load)" is a very important thing to remember. When working with Movie Clips, we will divide our code into two separate parts using the "onClipEvent" handlers. "onClipEvent(load)" is the code that runs only once when the object first loads. This is where we define all the variables that the object will use. In this case, we set a variable that will be the speed of our spaceship.
The second part of the code is contained in a section called "onClipEvent(enterFrame)". This is the code that loops over and over again. This is the main part of the code, where we include everything we want the object to do while it's running.
In this case, we want to be able to move the ship with the keyboard. So just below the code you just wrote, add this:
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}
}
The code is pretty self explanitory. "if(Key.isDown)" checks to see if a certain key on the keyboard is pressed. _x and _y refer to the X and Y position of the object. All the code means is that, if the button is pressed, the object moves the amount of pixels we specified with our speed variable.
Adjust the speed variable in the first part of the code to determine how fast the ship will go.
I know, I know, the ship can fly off the screen, but that isn't important right now. We're going to set movement boundaries at the end in "Finishing Touches". As long as the ship can move, we're fine for now.
Now that you have a completed spaceship, the last step is to give it an instance name using the properties tab. This is the name that other pieces of code can use to refer to this object. I used the instance name player for simplicity.
Step 3 - Equipping Your Weapon
Flying around is great, but it isn't going to help us much when the evil spaceships attack us. We need to equip our badass space cruiser with some serious weaponry. It's time to use your drawing skills again, we're going to be drawing the laser/missile/torpedo/bomb/whatever that your spaceship is going to fire.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/003.png
Here's mine, a deadly green bolt of pure nuclear-electro-antimatter-protonic laser energy. If that's not enough to stop our enemies, I don't know what is. Remember to keep your drawing small, you don't want the shot to be bigger than your ship. Or maybe you do, that would kick a lot of ass I suppose. But keep it reasonable.
Now convert it to a Movie Clip, and give it the instance name shot. Now move it off the stage. Tuck it away where you wont see it during the game, and I'll explain why.
The Pirate has developed a method of making games where he creates "stock objects", places all the code they need inside them, and then moves them off the screen. Then, when an object such as a shot is needed, I use a code that makes a duplicate of the stock object wherever it is needed. That way, I can keep track of all my objects easily and "summon" copies of them onto the screen whenever one is needed.
By no means is this the "right" way to make a game. It's probably not even the best way either, but that's the way I make games. If you want to learn a different method, you're reading the wrong tutorial.
Now it's time to add the code to the shot itself. Click on it, open the actions tab, and write this:
onClipEvent (load) {
speed = 10;
}
onClipEvent (enterFrame) {
_y -= speed;
}
The code is entirely self explanitory. You choose a speed for the projectile, and it travels upwards at that speed.
The complicated code is the code that allows you to fire your weapon. First we need to define the variables used by the code. Click on your ship again, and add this code inside the existing onClipEvent(load).
shotnum = 10;
shotmin = 10;
shotmax = 20;
reload = 8;
nextshot = 0;
I'll explain what each of these variables does. First though, put the actual firing code inside the onClipEvent(enterFrame) section. The code is as follows:
if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}
So what does all that mess mean? First things first, we check to see if our fire key is pressed, in this case the spacebar. Then we come to our first variable, nextshot. This represents the cooldown timer of the gun, we can only fire the gun when the cooldown is at 0. This prevents us from firing a continuous stream.
You may have noticed the double equal sign, == in the code. This is not a typo, it is very important. In Actionscript, we use a single equal sign to define a variable, and a double equal sign to make a comparison. In this case we are testing to see if nextshot equals zero, so we use a double. The code will not work with a single equal sign, try it and see.
Once the code has checked to make sure we are allowed to fire a shot, we come to the code that actually makes the projectile. nextshot = reload; will reset the cooldown timer to our reload variable. Changing the variable will give us a different reload time.
Shotnum++; is just a simple way of increasing the variable shotnum by 1. shotnum is what determines the depth of the shot we are about to create.
In Flash, all objects that are created by code (such as the duplicate we are about to make) have a depth. An objects depth is unique, and no two objects can have the same depth at any one time. By using a constantly increasing variable shotnum we ensure that none of our shots cancel eachother out.
Now the fun part, firing the shot. _root.shot.duplicateMovieClip("shot"+shotnum, shotnum); is the code that creates the duplicate.
_root.shot tells the code which object to duplicate, the _root. is just a way of indicating to Flash that the object exists on the main stage. If the object existed inside of another object called "box" for example, you would write _root.box.shot. This just tells Flash where the object is.
duplicateMovieClip creates a duplicate, with the parameters we specify in the brackets, ("shot"+shotnum, shotnum). The first parameter is the instance name of the new object. We want each projectile to have a unique instance name so we call it "shot" and add shotnum to the end of it. Make sure to include the quotation marks around "shot", this tells Flash that we are dealing with a string of text, and not a variable or an object.
The second parameter is the depth, we set it to our variable shotnum.
Next we have to place our new duplicate at the position of our ship. _root["shot"+shotnum]. is a way of referring to the duplicate we just created. Everything in the square brackets is treated as a single instance name. so ["someobject"+30] would refer to someobject30. Then we simply set the _x and _y of the duplicate to the position of our ship.
The final pieces of the code just regulate our variables.
if (nextshot>0) {
nextshot--;
}
Will reduce the cooldown timer if it is anything more than 0.
if (shotnum>=shotmax) {
shotnum = shotmin;
}
Will keep shotnum in between a certain range that we set ahead of time. This prevents shotnum from just increasing infinitely, and possibly cancelling out other objects that might be using that depth. For the example game, I've set a range of 10-20, allowing 10 shots to be on the screen at once. If you made your gun fire like a machine gun, you might need to have more than 10 shots on the screen. Adjust your min and max values accordingly.
TestMovie to try out your gun and make sure you did everything right. Then move on to the next step.
Step 4 - Spawning Enemy Spaceships
Now that you've got a powerful weapon mounted on your ship, you're probably itching to shoot at something. It's time to create a legion of enemies for you to fight. Start by once again designing some art, draw the meanest looking enemy spaceship you can imagine. I went for that retro Space Invaders look.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/enemy.png
Once again, convert it to a Movie Clip, give this one the instance name enemy.
Now, there are infinite possibilities when designing the enemies of your game. You could design enemies that fire lasers at you, floating space mines, giant boss ships, torpedo weilding death warships, anything you want. Obviously, I can't cover all that right now. Pirate's Space Game Tutorial, Part 2 will deal with extra fun stuff like enemies that shoot back, or dodge bullets.
The enemy we will be designing in this tutorial is the simplest type. The kamikaze enemy. We are going to program them to fly stupidly down the screen, and hope they hit you.
You should be able to figure the code out already, if you've been learning anything from this tutorial. We'll set up some variables, and then add movement code. Like this:
onClipEvent (load) {
speed = 4+random(5);
shield = 3;
}
onClipEvent (enterFrame) {
_y += speed;
}
To mix things up a little, I've made the enemies go at a random speed. I set 4 as the base speed, plus random(5). Remember that Flash counts from 0 when it chooses random numbers, so random(5) is really picking a number from 0-4.
Don't worry about the shield variable, that will be used later.
Now that we have our basic enemy, put it away offscreen like we did with the shot. We're going to duplicate this base enemy whenever we need to spawn one. To start spawning enemies, we're going to use a technique I always use. Draw a circle/square/scribble anything really, somewhere off screen and convert it to a movie clip. Give it an instance name like spawner.
This hidden movie clip offscreen is going to spawn enemies. You'll understand how in a bit. Click on the spawner and add this code to it:
onClipEvent (load) {
nextenemy = 0;
enemynum = 30;
enemymin = 30;
enemymax = 50;
}
onClipEvent (enterFrame) {
if (nextenemy == 0) {
nextenemy = 15+random(40);
enemynum++;
_root.enemy.duplicateMovieClip("enemy"+enemynum, enemynum);
_root["enemy"+enemynum]._x = random(_root.stagex);
_root["enemy"+enemynum]._y = -40;
}
if (nextenemy>0) {
nextenemy--;
}
if (enemynum>=enemymax) {
enemynum = enemymin;
}
}
I shouldn't have to explain much of this code, it's essentially the same as the code for firing shots. A variable called nextenemy counts down. When it reaches 0, it is reset to a random number. Then a duplicate is made of the original enemy.
_root["enemy"+enemynum]._x = random(_root.stagex); makes use of our global variable. _root is used again to tell Flash where the variable is found. Then the position of the enemy is set to a random _x value within the confines of our stage.
_root["enemy"+enemynum]._y = -40; places the enemy at -40y, just above the stage, so it can fly downwards. If your enemy is particularly tall, you may want to put it even farther away.
The next pieces of code are right from our projectile firing code. They regulate enemynum, which keeps the depth of our enemies between 30-50, so they don't interfere with our shots, which are in 10-20. Once again, if you want to make enemies spawn even more frequently, you may need to change some of these parameters. The code is easy to play with so it will work with your game.
Once again, TestMovie. If you did everything right, you should see enemies spawning and flying at you.
CHARACTER LIMIT, TUTORIAL CONTINUED IN NEXT POST.
You guys asked for it, and here it is. The Pirate's big tutorial on making a space game in Flash. In part 1 of the tutorial I'm going to walk you through the absolute basics. By the end you will have a very simple little space game ready to be expanded on.
This part of the tutorial will be done in a few main sections:
Getting started
Constructing your spaceship
Equipping your weapon
Spawning enemy spaceships
Collision Detection
Finishing touches
Before we start, I should make something clear. This tutorial assumes that you are already familiar, and reasonably skilled with the Flash program itself. You should know things like, where the actionscript tab is, where the properties tab is, how to set an instance name, how to create and work with movie clips, and so on. I'm not going to explain precisely, step by step, which buttons to press and what to click on. You need to know what you're doing.
If you meet the above criteria, you're ready to start the tutorial.
Step 1 - Getting Started
We're going to start by making a major decision that will affect how you go about creating everything else in the game. Do we want our game to be a top scrolling, or a side scrolling game?
For my example game, I am going to be making a top scroller. the things you learn in this tutorial will allow you to create either one, you just need to use some common sense and change the code around.
Just remember this:
In Flash, and basically every program you will ever come across, positions are measured in X and Y values. The X value is the horizontal position, the Y value is the vertical postition. I'm going to assume you already have SOME knowledge of how coordinates work.
If for example, I am making an object move upwards, I may type some code that deals with the Y-value. If you are making a side scrolling shooter, you will probably want that same object to be travelling sideways in your game, so you use the same code that I type BUT use the X-value instead. Just use some common sense and play with the code so it works with what you're trying to do.
For reference, an X value of 0 is the very left edge of your stage, and a Y value of 0 is the very top of the stage.
Alright, so since I'm making a top scroller, I'm probably going to want my stage to be tall and narrow. You can make your game any size you want, in general, you want a side scroller to be a short wide rectangle, and the opposite for a top scroller.
I'm going to use a width of 300, and a height of 400.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/001.png
While I'm here, I also set the background to black, so it will look more like space. Obviously you don't HAVE to set your game in space. Draw an elaborate scrolling background if you want, be creative.
Next, we're going to set the frame rate. I use 30fps for games to make them run smoothly, you can use anything you want.
Now that the stage is set up, we're going to set two Global Variables. Global variable is the name I use to refer to variables on the main timeline, instead of in an object. You should have a single blank keyframe on your timeline, click on it, then open the Actionscript tab. Enter this code:
stagex = *the width you set for your game*;
stagey = *the height you set for your game*;
NOTE: I use "stagex" and "stagey" for simplicity, but you can name your variables anything you want. The name of the variable doesn't actually matter, since we decide what the variable does when we actually write code.
These variables will be infinitely useful if you want to change your game later on. For example, lets say the height of my game was 300. I have some code that makes enemies dissapear when they go past the 300 mark, to make sure they don't keep going when they go off the screen. Later on, I decide the game screen is too short, and want to make the game 500 pixels high instead.
That means I have to go to each enemy and change it's code to use 500 instead. But, if I'm using global variables in my code, it's as easy as clicking on the timeline, and changing the global.
Now that your stage is set up, and it's dimensions have been noted in your global variables, you're ready to start making the game.
Step 2 - Constructing Your Spaceship
Now that we have a rectangle of pixelated space to work with, we need a spaceship so we can fly around in it. It's time to use your insane Flash drawing skills. Draw the most badass space cruiser you can imagine.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/002.png
Here's mine, I opted for the sleek triangular look and painted it red to make it go faster. Remember to keep your spaceship reasonably small. Once you've drawn it, you'll want to convert it into a Movie Clip. All of the objects in our space game will be movie clips, that way we can add code to them.
And that's exactly what it's time to do. We've got to write some code to allow us to fly our new spaceship.
Select your spaceship and open the actions tab. Enter this code:
onClipEvent(load){
speed = 5;
}
"onClipEvent(load)" is a very important thing to remember. When working with Movie Clips, we will divide our code into two separate parts using the "onClipEvent" handlers. "onClipEvent(load)" is the code that runs only once when the object first loads. This is where we define all the variables that the object will use. In this case, we set a variable that will be the speed of our spaceship.
The second part of the code is contained in a section called "onClipEvent(enterFrame)". This is the code that loops over and over again. This is the main part of the code, where we include everything we want the object to do while it's running.
In this case, we want to be able to move the ship with the keyboard. So just below the code you just wrote, add this:
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}
}
The code is pretty self explanitory. "if(Key.isDown)" checks to see if a certain key on the keyboard is pressed. _x and _y refer to the X and Y position of the object. All the code means is that, if the button is pressed, the object moves the amount of pixels we specified with our speed variable.
Adjust the speed variable in the first part of the code to determine how fast the ship will go.
I know, I know, the ship can fly off the screen, but that isn't important right now. We're going to set movement boundaries at the end in "Finishing Touches". As long as the ship can move, we're fine for now.
Now that you have a completed spaceship, the last step is to give it an instance name using the properties tab. This is the name that other pieces of code can use to refer to this object. I used the instance name player for simplicity.
Step 3 - Equipping Your Weapon
Flying around is great, but it isn't going to help us much when the evil spaceships attack us. We need to equip our badass space cruiser with some serious weaponry. It's time to use your drawing skills again, we're going to be drawing the laser/missile/torpedo/bomb/whatever that your spaceship is going to fire.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/003.png
Here's mine, a deadly green bolt of pure nuclear-electro-antimatter-protonic laser energy. If that's not enough to stop our enemies, I don't know what is. Remember to keep your drawing small, you don't want the shot to be bigger than your ship. Or maybe you do, that would kick a lot of ass I suppose. But keep it reasonable.
Now convert it to a Movie Clip, and give it the instance name shot. Now move it off the stage. Tuck it away where you wont see it during the game, and I'll explain why.
The Pirate has developed a method of making games where he creates "stock objects", places all the code they need inside them, and then moves them off the screen. Then, when an object such as a shot is needed, I use a code that makes a duplicate of the stock object wherever it is needed. That way, I can keep track of all my objects easily and "summon" copies of them onto the screen whenever one is needed.
By no means is this the "right" way to make a game. It's probably not even the best way either, but that's the way I make games. If you want to learn a different method, you're reading the wrong tutorial.
Now it's time to add the code to the shot itself. Click on it, open the actions tab, and write this:
onClipEvent (load) {
speed = 10;
}
onClipEvent (enterFrame) {
_y -= speed;
}
The code is entirely self explanitory. You choose a speed for the projectile, and it travels upwards at that speed.
The complicated code is the code that allows you to fire your weapon. First we need to define the variables used by the code. Click on your ship again, and add this code inside the existing onClipEvent(load).
shotnum = 10;
shotmin = 10;
shotmax = 20;
reload = 8;
nextshot = 0;
I'll explain what each of these variables does. First though, put the actual firing code inside the onClipEvent(enterFrame) section. The code is as follows:
if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}
So what does all that mess mean? First things first, we check to see if our fire key is pressed, in this case the spacebar. Then we come to our first variable, nextshot. This represents the cooldown timer of the gun, we can only fire the gun when the cooldown is at 0. This prevents us from firing a continuous stream.
You may have noticed the double equal sign, == in the code. This is not a typo, it is very important. In Actionscript, we use a single equal sign to define a variable, and a double equal sign to make a comparison. In this case we are testing to see if nextshot equals zero, so we use a double. The code will not work with a single equal sign, try it and see.
Once the code has checked to make sure we are allowed to fire a shot, we come to the code that actually makes the projectile. nextshot = reload; will reset the cooldown timer to our reload variable. Changing the variable will give us a different reload time.
Shotnum++; is just a simple way of increasing the variable shotnum by 1. shotnum is what determines the depth of the shot we are about to create.
In Flash, all objects that are created by code (such as the duplicate we are about to make) have a depth. An objects depth is unique, and no two objects can have the same depth at any one time. By using a constantly increasing variable shotnum we ensure that none of our shots cancel eachother out.
Now the fun part, firing the shot. _root.shot.duplicateMovieClip("shot"+shotnum, shotnum); is the code that creates the duplicate.
_root.shot tells the code which object to duplicate, the _root. is just a way of indicating to Flash that the object exists on the main stage. If the object existed inside of another object called "box" for example, you would write _root.box.shot. This just tells Flash where the object is.
duplicateMovieClip creates a duplicate, with the parameters we specify in the brackets, ("shot"+shotnum, shotnum). The first parameter is the instance name of the new object. We want each projectile to have a unique instance name so we call it "shot" and add shotnum to the end of it. Make sure to include the quotation marks around "shot", this tells Flash that we are dealing with a string of text, and not a variable or an object.
The second parameter is the depth, we set it to our variable shotnum.
Next we have to place our new duplicate at the position of our ship. _root["shot"+shotnum]. is a way of referring to the duplicate we just created. Everything in the square brackets is treated as a single instance name. so ["someobject"+30] would refer to someobject30. Then we simply set the _x and _y of the duplicate to the position of our ship.
The final pieces of the code just regulate our variables.
if (nextshot>0) {
nextshot--;
}
Will reduce the cooldown timer if it is anything more than 0.
if (shotnum>=shotmax) {
shotnum = shotmin;
}
Will keep shotnum in between a certain range that we set ahead of time. This prevents shotnum from just increasing infinitely, and possibly cancelling out other objects that might be using that depth. For the example game, I've set a range of 10-20, allowing 10 shots to be on the screen at once. If you made your gun fire like a machine gun, you might need to have more than 10 shots on the screen. Adjust your min and max values accordingly.
TestMovie to try out your gun and make sure you did everything right. Then move on to the next step.
Step 4 - Spawning Enemy Spaceships
Now that you've got a powerful weapon mounted on your ship, you're probably itching to shoot at something. It's time to create a legion of enemies for you to fight. Start by once again designing some art, draw the meanest looking enemy spaceship you can imagine. I went for that retro Space Invaders look.
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/enemy.png
Once again, convert it to a Movie Clip, give this one the instance name enemy.
Now, there are infinite possibilities when designing the enemies of your game. You could design enemies that fire lasers at you, floating space mines, giant boss ships, torpedo weilding death warships, anything you want. Obviously, I can't cover all that right now. Pirate's Space Game Tutorial, Part 2 will deal with extra fun stuff like enemies that shoot back, or dodge bullets.
The enemy we will be designing in this tutorial is the simplest type. The kamikaze enemy. We are going to program them to fly stupidly down the screen, and hope they hit you.
You should be able to figure the code out already, if you've been learning anything from this tutorial. We'll set up some variables, and then add movement code. Like this:
onClipEvent (load) {
speed = 4+random(5);
shield = 3;
}
onClipEvent (enterFrame) {
_y += speed;
}
To mix things up a little, I've made the enemies go at a random speed. I set 4 as the base speed, plus random(5). Remember that Flash counts from 0 when it chooses random numbers, so random(5) is really picking a number from 0-4.
Don't worry about the shield variable, that will be used later.
Now that we have our basic enemy, put it away offscreen like we did with the shot. We're going to duplicate this base enemy whenever we need to spawn one. To start spawning enemies, we're going to use a technique I always use. Draw a circle/square/scribble anything really, somewhere off screen and convert it to a movie clip. Give it an instance name like spawner.
This hidden movie clip offscreen is going to spawn enemies. You'll understand how in a bit. Click on the spawner and add this code to it:
onClipEvent (load) {
nextenemy = 0;
enemynum = 30;
enemymin = 30;
enemymax = 50;
}
onClipEvent (enterFrame) {
if (nextenemy == 0) {
nextenemy = 15+random(40);
enemynum++;
_root.enemy.duplicateMovieClip("enemy"+enemynum, enemynum);
_root["enemy"+enemynum]._x = random(_root.stagex);
_root["enemy"+enemynum]._y = -40;
}
if (nextenemy>0) {
nextenemy--;
}
if (enemynum>=enemymax) {
enemynum = enemymin;
}
}
I shouldn't have to explain much of this code, it's essentially the same as the code for firing shots. A variable called nextenemy counts down. When it reaches 0, it is reset to a random number. Then a duplicate is made of the original enemy.
_root["enemy"+enemynum]._x = random(_root.stagex); makes use of our global variable. _root is used again to tell Flash where the variable is found. Then the position of the enemy is set to a random _x value within the confines of our stage.
_root["enemy"+enemynum]._y = -40; places the enemy at -40y, just above the stage, so it can fly downwards. If your enemy is particularly tall, you may want to put it even farther away.
The next pieces of code are right from our projectile firing code. They regulate enemynum, which keeps the depth of our enemies between 30-50, so they don't interfere with our shots, which are in 10-20. Once again, if you want to make enemies spawn even more frequently, you may need to change some of these parameters. The code is easy to play with so it will work with your game.
Once again, TestMovie. If you did everything right, you should see enemies spawning and flying at you.
CHARACTER LIMIT, TUTORIAL CONTINUED IN NEXT POST.