This tutorial will show you how to make a fish move and fire in flash. It is different than the last spaceship move and fire because it is a bit more complex. We will cover how to do inertia and how to make an object turn around without losing its former speed. We will also discuss how to make a bullet fire the way the fish is facing. There are a couple of new concepts that are going to be taught in this tutorial. If you want to grab the source and walk along with me, you can go Here and grab it.
The final product should look like this(arrows to move, space to fire):
Now first, make 2 layers in the game. Call one layer background(the bottom layer) and one layer fish(the top layer). We will have the background in the background layer and the fish in the fish layer. You can lock layers to prevent editing in the wrong layer. See example picture below:
Now lock the fish layer, and use the rectangle tool to draw a blue background to look like water(this wil depend on your artistic skills).
Now lock the background layer and draw the fish and the bullet.
First of all, you must draw a fish. After you have drawn one, go to modify-->convert to symbol-->Movie clip and convert it to a movie clip. Now name the movie clip "fish1". Next draw a circle for the bullet of the fish and also convert it to a symbol of type movie clip also. Call this clip "shot1" both when you name the symbol and in the properties box(this must be done) . See the reference pictures below:
Now select the fish and open the actions box and type in this code:
Code:
onClipEvent (load)
{
_root.shot1._visible=false;
horizspeed=0;
verticalspeed=0;
addspeed = 1;
decay = .8;
maxSpeed = 10;
isright=1;
isdown=0;
this._x=50;
this._y=100;
spacedown=0;
shot1count=1;
}
This code sets some important variables for the fish. The first line is more for the bullet. Since we don't want the initial bullet just random zooming across the screen when the movie loads, we set the intial bullet we drew to be not visible. Next we set the horizontal speed and vertical speed of the fish initially. Since intially we want our fish to not be moving when the movie loads, we set both these to zero. Addspeed is how fast our fish will increase in speed once we hold down one of the arrow keys, 1 is a pretty good number for acceleration purposes. Decay is how fast the fish will slow down when we let go of the key. The value .8 means if we let go of the key, every frame, the speed will only be 80% of what it was last frame, this is a fairly fast slowing speed. Maxspeed needs to be set in most games with these kinds of controls because you do not want an object traveling at the speed of light across the screen, so maxSpeed is the maximum possible X or Y speed that the fish can go at. The variable isright and isdown are used to keep track of which way the fish is currently going and for isright, its used to track if the fish is currently facing right or left for shot direction. this._x and this._y are the initial coordinates of the fish. A standard flash document is 550 by 400 so you can set these accordingly. The variable shot1count is used to keep track of how many shots are fired. This is important for duplication and depth purposes. Spacedown is a variable I use to track if the spacebar is down. In this tutorial example, I am going to show you how to not let a player rapid fire simply by holding the spacebar down.
Now that we have set some initial variables for the fish, lets go to the code that actually moves the fish. So select your fish movie clip again, open the action box, and add this code under the code that is already there:
So this defines the movement code for the fish. For this tutorial, the arrow keys are movement and space is to fire. Lets take a closer look at what happens when you press one of the arrow keys by looking at the code for what happens when you push the right arrow key:
Code:
if(Key.isDown(Key.RIGHT))
{
if (isright==1)
{
if(horizspeed
{
horizspeed+=addspeed;
}
}
else
{
_xscale = -(_xscale);
isright=1;
}
}
Key.isDown(Key.RIGHT) detects if the right key has been pressed of not. If it has, it goes to the code. First we check to see if isright(whether the fish is facing right currently) is true or false(zero is false, one is true). If it is not one, then since you are pressing right, it will change it to one and use the code _xscale=-(_xscale) to flip the fish horizontally and then set isright to 1 to indicate that the fish is now facing right. Since the movie checks every frame to see if we are holding the right button down, if you keep holding right down, it will just accelerate the fish right. The left button works the exact same way as the right button, except it checks to see if isright is 0 and does the opposite flips. Before it increments the speed, the code also checks to see if the horizontal speed is great than the maximum speed(10), it will only add speed if the current speed is less than 10. For left, it checks to see if the speed is greater than -10(10 in the other direction) and does the same.
The up and down buttons work the same way, except instead of isright, the code checks for isup to see if the fish is moving up or not. Instead of changing horizontal speed, it changes vertical speed. There is no need to flip the fish with the up and down arrows, because fish do not change facing directions whether moving up or down.
There is one additional check for vertical speed. If the vertical position of the fish is 375 or less than 25, the fish will automatically slow down so it does not move off the screen. For horizontal movement, it does something different, it makes the X position 0 if the X position is greater than 510, and it makes the X position 505 if X is less than -5. This basically means if the fish goes off the screen one way, it appears on the other side, nifty!
If space is pressed it checks to see if spacedown is false(that space bar is not currently being held down). It shoots only if it is not. It duplicates the shot1 movie clip(the fish bullet), names it shot1 plus the incremental number in shot1count(so we don't have duplicate names) and sets it to a depths of shot1count(two clips cannot have the same depth). Then it increments shot1count so the next shot has a different name and depth. Finally it sets spacedown to 1 so it won't auto-fire by holding down space.
Outside this, if space isn't being pressed it sets spacedown back to zero so you can fire again.
Now that we've fired the bullet, we need to make it move. So select the bullet clip, open the actions box and type this in:
Code:
onClipEvent (load)
{
if(_root.fish1.isright==1)
{
this._y=_root.fish1._y;
this._x=_root.fish1._x;
speed=40;
}
else if(_root.fish1.isright==0)
{
this._y=_root.fish1._y;
this._x=_root.fish1._x;
speed=-40;
}
}
onClipEvent(enterFrame)
{
this._x+=speed;
if(this._x<0)
{
this.removeMovieClip();
}
if(this._x>550)
{
this.removeMovieClip();
}
}
So first of all, when each bullet loads, it checks to see if the fish is facing right or not, if it is, it sets the speed to 40(going right), if it is not, it sets the speed to -40 which means 40 going left. In both cases it sets the initial x and y coordinates to those of the fish. Then in the enterFrame event, it increments the x position by the speed defined in the load event of the code. It removes the movie clip if the X position is greater than 550 or if the x position is less than 0, meaning the clip has moved off the screen.
Thats it for making a fish move and fire!.
Any questions about this tutorial, please discuss them in Here .