Hey
Welcome to the new and awaited lesson about flash gamemaking! I know you all have been awaiting for the real stuff to begin, that’s it!
First of all, you have learned to control your main sprite with mouse and keyboard. So you are going to program a little application that will:
- Control your main character
- Create and control enemies
- Check general collisions - i.e. the events that happen when some char on the screen is hit by something else, like projectiles, enemies themselves, bonus or game screen bounds.
- Decrease or increase several variables attributed to some values. For example, score, energy, ammunition number.
- Check every now and then for special events to happen, like death of the main character, enemy destroyed, game over, game exit or anything.
This is the skeleton of your game.
Now we are focusing on a simple game for this lesson. We are programming something like Money Money, a game from a old (but very dear) site I worked on, the site was Naive.it. The game sees a piggy bank, that is a money box shaped like a pig, chasing money to increase its points, and trying to avoid hammers falling from the sky, that will make you loose energy. When energy is over, you are over. Otherwise it will go on forever. Let’s start doing something like this. We prefer using mouse controls for this game. This will leave the player the possibility to make the main char move faster than what we could achieve with keyboard.
Create a new fla document. Set the stage dimensions to 400×400 pixels at 30fps.
Start creating your character. We are not loosing time on making graphics first, so we are going to use a simple square for the main character. We are using the code for mouse control for it, so we are going to use onEnterFrame to check on every frame for the mouse position, and changing the sprite position as well. We can use a simple square movieclip, with pivot point set to its center, and we can give it the instance name “charâ€.
Place the char onto the stage, leaving it in the bottom part at the center. I will leave some pixels as the bottom margin.
Now, in the timeline, add a new layer, name it “actionsâ€, block it and then add this code to control the char sprite.
stop();
char.onEnterFrame = function () {
dx = this._x - _xmouse;
this._x -= dx/10;
}
Please test your game, you will see your square moving right and left as you move your mouse. You can notice two things.
- The square does not move vertically. This is because for this game, the sprite does not have to move but horizontally.
- If you drag your mouse out of the stage, the square follows it and get out of the stage. So we are going to implement some limiting in the horizontal movement.
We can change the code this way:
char.onEnterFrame = function () {
dx = this._x - _xmouse;
this._x -= dx/10;
if (this._x > 370) {
this._x = 370;
}
if (this._x < 30) {
this._x = 30;
}
}
So now, if we test the swf, we can see that the square will still follow the mouse, but it will stop at the right and left limits of the stage.
That’s almost all for the main char movement.
Now we are going to create the object dispatcher.
What is the object dispatcher? Well, it is just a fantasy name for the movieclip that will handle coin creation. This time it will summon a coin to fall from the ceiling every n milliseconds. In other kind of games, maybe this will help creating an enemy dispatcher, that will serve your game with enemy created infinitively.
Anyway. The method is quite easy. Making it actionscript, it more difficult but still not impossible.
We must create a symbol in library, with linkage name “coinâ€. Every n milliseconds, the dispatcher will create a clone of “coin†and attach it to the stage. After attaching it, each coin should fall, so we are going to create the code for each coin to make it move. Moreover, we should evaluate collisions of coins with the character, to increase the score.
But the dispatcher has an evil side too. It will create not only coins, but even hammers, that can damage you, if they hit.
So, let’s start. Create a simple star using the polystar tool (or by hand). Create a blue star and a red star. We are going to use the blue one as a coin, and the red one as a hammer. So please make them movieclips and give them the linkage name of “coin†and “hammerâ€. Now you can delete them from the stage, they are not needed anymore.
Ops. Time has finished :D
Next time we are going to see how to implement the mechanic we discuss about the dispatcher in actionscript. Prepare yourself, next one will be a tough one!
See you and experiment by yourself!
