Subscribe to this Blog

Creating a Flash Shooter game like SkyForce: Part 2

By admin • Aug 30th, 2008 • Category: Tutorials

I’m proud to present the second part of this tutorial. In this section I’m covering the motion and shooting of our ship. If you haven’t already read the first part I suggest you go here.

First let’s talk about the shooting, I like shooters where the ship shoots continuously in a direction and keeps getting power-ups to increase the shot intensity. So I’m going to show how to make the ship fire continuously.

Movement is simple enough. The arrow keys to move has always been the standard procedure and I’m not about to change that. There is one more symbol that I’ve used in the game this time around:

SYMBOLS:
shot: the symbol used for shots.
This here is the code. I’ve explained all the lines I added this time around. The others are explained in part 1.

  1. stop();
  2. xspeed = 0;
  3. yspeed = 0;
  4. friction = 0.8;
  5. turntest = 0;
  6. shotcount = 0;
  7. shottime = 10;
  8. motionspeed = 1;
  9. _root.attachMovie("back1", "back1", _root.getNextHighestDepth(), {_x:250, _y:0});
  10. _root.attachMovie("back2", "back2", _root.getNextHighestDepth(), {_x:250, _y:-400});
  11. _root.createEmptyMovieClip("shotmovie", _root.getNextHighestDepth());
  12. _root.attachMovie("ship", "ship", _root.getNextHighestDepth(), {_x:250, _y:200});
  13. ship.onEnterFrame = function() {
  14.     _root.shipmover();
  15.     _root.shooter();
  16.     _root.mover();
  17. };
  18. function shipmover() {
  19.     ship._x += xspeed;
  20.     ship._y += yspeed;
  21.     if (ship._x<10) {
  22.         ship._x = 10;
  23.     }
  24.     if (ship._x>490) {
  25.         ship._x = 490;
  26.     }
  27.     if (ship._y<40) {
  28.         ship._y = 40;
  29.     }
  30.     if (ship._y>390) {
  31.         ship._y = 390;
  32.     }
  33.     xspeed = xspeed*friction;
  34.     yspeed = yspeed*friction;
  35.     if (Key.isDown(Key.UP)) {
  36.         yspeed--;
  37.     }
  38.     if (Key.isDown(Key.DOWN)) {
  39.         yspeed++;
  40.     }
  41.     if (Key.isDown(Key.LEFT)) {
  42.         turntest++;
  43.         xspeed--;
  44.     }
  45.     if (Key.isDown(Key.RIGHT)) {
  46.         turntest--;
  47.         xspeed++;
  48.     }
  49.     if (turntest != 0) {
  50.         ship._width = 35;
  51.     } else {
  52.         ship._width = 40;
  53.     }
  54.     turntest = 0;
  55. }
  56. function shooter() {
  57.     shottime--;
  58.     if (shottime == 0) {
  59.         if (shotcount == 7) {
  60.             shotcount = 0;
  61.         }
  62.         shottime = 10;
  63.         shot = shotmovie.attachMovie("shot", "shot"+shotcount, shotmovie.getNextHighestDepth(), {_x:ship._x, _y:ship._y-14});
  64.         shotcount++;
  65.         shot.speed = 8;
  66.         shot.onEnterFrame = function() {
  67.             this._y -= this.speed;
  68.             if (this._y<0) {
  69.                 this.removeMovieClip();
  70.             }
  71.         };
  72.     }
  73. }
  74. function mover() {
  75.     back1._y += motionspeed;
  76.     back2._y += motionspeed;
  77.     if (back1._y>=400) {
  78.         back1._y = -400;
  79.     }
  80.     if (back2._y>=400) {
  81.         back2._y = -400;
  82.     }
  83. }

Line by line explanation of the code

2. “xpseed” controls the horizontal speed of the ship.

3. “yspeed” controls the vertical speed of the ship.

4. “friction” is the amount of stopping force I’m applying on the ship.

5. “turntest” checks if the ship is banking to morph its shape.

6. “shotcount” the number of shots on screen/loaded. I use this so that later on I can make an array of shots to check for hittests.

7. “shottime” the time delay between two consecutive shots.

11. Adds an empty movieclip onto the stage that acts as a container for shots.

14, 15. Calls functions required for shooting and movement.

18. Ship movement function.

19- 20. Moves the ship according to the speeds

21- 32. Limits the ship into the stage.

33, 34. Decreases the speed according to the friction.

35- 48. Checks for arrow key presses and changes the speed accordingly. Toggles “turntest” variables if horizontal movement is there.

49-55. Manages the ship’s “turning look” ie how the ship looks when it banks.

56. Function that handles shooting.

57. Decreases the shottime variable every frame.

58. Checking if shottime has reached zero so as to initiate the shooting process.

59-61 .Making sure that the value of shotcount always remains between 0 and 7.

62. Increasing shottime interval for the next iteration.

63. This attaches a new movieclip by the name of “shot”+shotcount (ie a unique name for each instance) and its attached on the emptymovieclip shotmovie.

65. Declares the shot speed.

66-71. The function that handles shots in every frame. Moves it forward each frame and removes it if it goes out of screen.

Any questions and comment… feel free to post em.

And here’s the result:

You can download the source files here.

If you liked this post, Buy me some beer (or coffee)

Tagged as:

admin is An engineering student from India. Enjoying his life both in the real world and in cyberspace
Email this author | All posts by admin

3 Responses »

  1. This is a really interesting blog post,I have added your blog to my bookmarks I really like it,keep up the good work!

  2. it so nice

  3. Great post, adding it to my bookmarks!

Leave a Reply