Parabolic motion (Actionscript 2.0)
Flash developer, c++ programmer
What I will show you here is only how to run the projectile.
1. Create two layers, one for actions, and the other for the graphics. Label the actions layer "Actions" for convenience. Make three keyframes for the actions layer, and three frames for the graphics layer. The purpose of the first frame is to initiate the variables, the second one for moving the projectile, and the third one for looping back to frame 2. Also, to make the movement smooth, set the frame rate to 100 fps.
2. Create the projectile; make a small, simple circle, and place it in the bottom-left corner, but not touching the bottom of the stage. Give it an instance name of bullet1.
3. Then, to adjust the attributes of the projectile, create two arrows (one up, one down), and one dynamic text, to adjust the angle. Then duplicate those for the adjustment of the initial speed. For the angle adjustment, name them up_angle, down_angle, and angle_txt. For the speed adjustment, name them up_spd, down_spd, and spd_txt. And last, make another movie clip named fire_bullet to fire the projectile after adjusting.
Your screen should look similar to this:
4. Then, write the code in frame 1 that initiates the variables of the projectile:
The angle variable specifies the angle, in degrees of the projectile.
The cur_speed variable indicates the initial speed of the release.
Now, this code:
Gets the x and y components of your projectile.
The g variable is the acceleration due to gravity, and the x_orig and y_orig variables just stores the initial position of bullet1.
5. Then, we move on to the second frame, we now write the code that moves the projectile:
_root.bullet1._x=_root.bullet1._x + (cur_speed_x/10);
_root.bullet1._y=_root.bullet1._y - (cur_speed_y/10);
cur_speed_y=cur_speed_y - g/50;
The first if condition stops the frame "loop" when the projectile hits the bottom of the stage, so that it doesn't continue falling below the screen. The "else" part is the one that makes the bullet move. According to the laws of physics, only the y-component of the speed decreases, so we decrease it with the acceleration due to gravity. I divided the acceleration by 50, since the flash movie is 100 fps, and the loop is two frames long. Anyway, you can set it at any value you want. Finally, the part after the if-else updates the dynamic text boxes of the values.
The next code, also in frame 2, should make the speed and angle adjustments work:
The code of the four arrows is easy to understand.
The fire_bullet's code just restores the projectile back to its original position, updates the x and y speeds of the projectile, and starts the loop again.
The last code to write, in frame 3 is to make the frame-loop:
6. Test the movie. Now, your timeline should look like this:
That's all. If you have any suggestions or questions, just leave comments. :) (No email please :D)