Kabomb > Tutorials > Enemy Spawning
Introduction
This technique uses linking from the library to set random positions and speeds for enemies, as it would be in a real game.
The Code
Create a new document in Flash and set the frame rate to about 24 fps. You can change this later if you want.

Draw a small spaceship and make it a movieclip. This will be the enemy. Delete it from the stage and open the library, so that you can see the enemy in there. Right click on the enemy movieclip, select "Linkage" and the tick "Export for Actionscript". In the identifier textbox, type "enemy" minus the quotation marks. Then on the forst frame of that document, type this into the actions:


Explanation

The first line sets the variable i to 0, and it will be our counter. The rest of the functions occur as each frame passes. Firstly, we add 1 to i each time by putting the ++ sign after it. The next line tests to see if Flash chooses 0 randomly out of 50 choices. This makes sure that at enemy is not created each frame and is the basis of the "random" enemies.

The next part includes the attachment of the enemy, where it starts and also where it goes. But first we'll make a movieclip out of the actions. We can do this by having "var MovieClipName: MovieClip". To link this to our enemy in the library, we first place an equals sign and attach the movie. The attachMovie functionhas three sets of data for you to enter (linkID, newname, depth). For our linkage we put "enemy", so instead of linkID we type"enemy". Because we want each copy of the enemy to have a different name, we make the newname '"enemy"+i', so one enemies name could be enemy23, depending on what i is. The next part is the depth. We make this i as well so that it is original. If any two or more movieclips on the screen had the same depth, they would not appear. This higher the depth means that they will be on top of everything else.

Now that we have attached the movieclip we'll need to set some properties. We set the x position of enemy to be randomly across the screen, we make it between 0 and whatever the width of the stage is inclusive. "Math.random()" selects a random number between 0 and 1, so it could be 0, 0.4662 or 1. After it selects this number, we multiply it by the width of the stage so that if it chose 1, the final answer would be 1xthe stage width, which is the edge of the stage, also our maximum.

Then we set the y position to be just below the bottom of the screen. The next line sets a variable relevant to the movieclip, and not relevant to the whole frame or layer. This is done by having enemy.speed. If we simply put speed, this variable would be the same for all enemies. Speed is selected randomly by having "Math.random()*5", which will result in a number from 0 to 5. Because we don't want the answer to be 0, we add 1 to that so that it could be from 1 to 6.

The next line is similar to the second, except we place "enemy." infront of "onEnterFrame=function(){". This is so we can control these movieclips from one location. The next line sets the movement of the movieclip. Each frame, the y position will move upward the amount of pixels whatever the variable "enemy.speed" is equal to. We have this so that the speed varies in each enemy. The last function checks to see if the movieclips y position is less than 0 (or above the screen). If it is, then the movieclip is removed because we don't need it any more.

If you want the enemies to come out more often or less often (there's currently a 1 in 50 chance of one coming out every frame), then change the 50. If the number is less, there will be more of a chance and if the number is more, there will be less of a chance. You can also change other variables such as the speed and try to make it so the enemies start on the right and move left.