AS: CANON
NOTE: I have designed this tutorial with the begining actionscriptor in mind, so I do a lot of in-depth explanation of code. If you feel that you are above this level, feel free to just skip around.
------------------------------------------
----------------
RELATED
AS: Main
AS: Variables
AS: Following/Shooting at Mouse
------------------------------------------
-----------------
Today you will be learning how to make a canon much like
this
Pardon the abysmal graphics, I just whipped it up in a few minutes. Use the UP and DOWN arrow keys to aim the canon, and press the SPACEBAR when the power meter is where you want it.
Creating a canon is actually quite simple once you break down the parts. So what do you need?
-You have to get the power
-You have to calculate the x and y components
-You have to make the canon shoot
NOTE: THE SAMPLE IS 30 FPS. CHANGE YOUR FRAMERATE TO 30 BEFORE FOLLOWING THIS TUTORIAL.
GETTING THE POWER (OR VELOCITY)
In the sample, there is a little power meter in the upper left corner. If you press the spacebar when the power meter is at the right, you get a lot of power in your shot. If you press it at the left, you get next to none.
NOTE: GO TO THE VIEW MENU, AND CLICK ON RULERS. THEY WILL PROVE TO BE USEFUL.
1) Start by opening a new document in Flash, and creating a rectangle (make it whatever color you wish, I used a liner gradient). Make it have a width of 200 pixels (you can type it in the Properties Inspector once you've drawn it).
Convert it to a movie clip (F8).
2) Put the rectangle where you want it to be on the stage (the center should be around 130 px.)
3) Not that we don't trust your positioning skills, but we're going to type in this code anyway:
onClipEvent(load) {
this._x=130
}
This will set the x coordinate to 130 pixels when the movie first loads up. Make sure you've typed this is the actions panel of the movie clip, and NOT a frame on the main timeline.
4) Draw a triangle, and convert it to a movie clip (F8). Give it an instance name of "pointer", and have it point to somewhere on your rectange.
NOTE: THE INSTANCE NAME CAN BE TYPED IN THE PROPERTIES INSPECTOR, AFTER SELECTING THE MOVIE CLIP.
5) Here's where the coding comes in (it goes in the actions panel of the movie clip). I'll walk you through.
onClipEvent (load) {
this._x=230;
decrease = true;
scan = true;
}
First, we set the pointer's x-coordinate to 230, which puts it at the end of the rectangle. We then define a few variables, which we will see again in a bit. This all happens when the MC (movie clip) first loads.
onClipEvent (enterFrame) {
if (scan==true) {
if (decrease == true) {
this._x -= 10;
} else if (decrease == false) {
this._x += 10;
}
if (this._x == 30) {
decrease = false;
} else if (this._x == 230) {
decrease = true;
}
}
The enterFrame tells Flash to run this script on every frame, rather than when it first loads.
We have set up a variable called decrease. When decrease is true, the pointer moves 10 pixels to the left each frame. Otherwise, if it is false, the pointer will move 10 pixels to the right. This results in a scrolling motion.
We then have to set up some boundries. If the pointer's x-coordinate reaches 30 (the far left) we tell Flash that decreasing is false, and the pointer begins to move to the right. Likewise, if it reaches 230 (the far right) we tell Flash that drecreasing is true, and it moves to the left again. We have created a scanning motion.
All of this happens while the variable "scan" is true.
if (Key.isDown(Key.SPACE)) {
scan = false;
}
power = (this._x-30)/10;
}
See where we're going with this? If the user presses the spacebar, we set the variable "scan" to false, so the pointer doesn't move anymore. Based on where the pointer is, we tell Flash what we want the power of our shot to be. We use the formula THIS._X-30/10.
this._x-30 will be a value between 0 and 200. Since the farthest our pointer can go on each side is 30 and 230, subtracting 30 just gives us a cleaner range. We divide by 10 to reduce the power, because a canonball move at 200 px. per frame would go too fast for our game.
Ta-da! We have our power! One down, two to go.
CONTINUED ON NEXT PAGE.....