Subscribe to this Blog

Creating a Flash Shooter Game like SkyForce: Part 3a

By sunil • Sep 4th, 2008 • Category: Tutorials

So its shooter time again. I’m back with part 3 where I’ve completed the prototype for the game. Whew!!
You can Check out part 1 and part 2 also.

Here’s the code.

  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. xmoved = 0;
  10. mlcounter = 0;
  11. misslecount = 0;
  12. xdist = new Array(20, 250, 260, 550, 560, 570, 850, 860, 870, 880, 1250, 1260, 1270, 1280, 1290, 1550, 1560);
  13. xplace = new Array(100, 410, 110, 380, 220, 125, 260, 300, 110, 350, 155, 300, 205, 65, 370, 105, 210);
  14. _root.attachMovie("back1", "back1", _root.getNextHighestDepth(), {_x:250, _y:0});
  15. _root.attachMovie("back2", "back2", _root.getNextHighestDepth(), {_x:250, _y:-400});
  16. _root.createEmptyMovieClip("shotmovie", _root.getNextHighestDepth());
  17. _root.createEmptyMovieClip("enemies", _root.getNextHighestDepth());
  18. _root.createEmptyMovieClip("bonusmovie", _root.getNextHighestDepth());
  19. _root.createEmptyMovieClip("missles", _root.getNextHighestDepth());
  20. _root.attachMovie("ship", "ship", _root.getNextHighestDepth(), {_x:250, _y:400});
  21. ship.onEnterFrame = function() {
  22.     _root.shipmover();
  23.     if (xmoved>1600) {
  24.         xmoved = 0;
  25.     }
  26.     _root.shooter();
  27.     _root.mover();
  28.     _root.renderer();
  29.     _root.hitenemy();
  30. };
  31. function shipmover() {
  32.     ship._x += xspeed;
  33.     ship._y += yspeed;
  34.     if (ship._x<10) {
  35.         ship._x = 10;
  36.     }
  37.     if (ship._x>490) {
  38.         ship._x = 490;
  39.     }
  40.     if (ship._y<40) {
  41.         ship._y = 40;
  42.     }
  43.     if (ship._y>390) {
  44.         ship._y = 390;
  45.     }
  46.     xspeed = xspeed*friction;
  47.     yspeed = yspeed*friction;
  48.     if (Key.isDown(Key.UP)) {
  49.         yspeed--;
  50.     }
  51.     if (Key.isDown(Key.DOWN)) {
  52.         yspeed++;
  53.     }
  54.     if (Key.isDown(Key.LEFT)) {
  55.         turntest++;
  56.         xspeed--;
  57.     }
  58.     if (Key.isDown(Key.RIGHT)) {
  59.         turntest--;
  60.         xspeed++;
  61.     }
  62.     if (turntest != 0) {
  63.         ship._width = 35;
  64.     } else {
  65.         ship._width = 40;
  66.     }
  67.     turntest = 0;
  68. }
  69. function mover() {
  70.     back1._y += motionspeed;
  71.     back2._y += motionspeed;
  72.     if (back1._y>=400) {
  73.         back1._y = -400;
  74.     }
  75.     if (back2._y>=400) {
  76.         back2._y = -400;
  77.     }
  78.     xmoved++;
  79. }
  80. function shooter() {
  81.     shottime--;
  82.     if (shottime == 0) {
  83.         if (shotcount == 7) {
  84.             shotcount = 0;
  85.         }
  86.         shottime = 10;
  87.         shot = shotmovie.attachMovie("shot", "shot"+shotcount, shotmovie.getNextHighestDepth(), {_x:ship._x, _y:ship._y-14});
  88.         shotcount++;
  89.         shot.speed = 8;
  90.         shot.onEnterFrame = function() {
  91.             this._y -= this.speed;
  92.             if (this._y<0) {
  93.                 this.removeMovieClip();
  94.             }
  95.         };
  96.     }
  97. }
  98. function renderer() {
  99.     for (i=0; i<17; i++) {
  100.         if (xmoved == xdist[i]) {
  101.             enemy = enemies.attachMovie("mlauncher", "mlauncher"+mlcounter, enemies.getNextHighestDepth(), {_x:xplace[i], _y:0});
  102.             enemy.health = 5;
  103.             enemy.shottime = 20;
  104.             enemy.onEnterFrame = function() {
  105.                 myRadians = Math.atan2(ship._y-this._y, ship._x-this._x);
  106.                 myDegrees = Math.round((myRadians*180/Math.PI));
  107.                 _root.yChange = Math.round(ship._y-this._y);
  108.                 _root.xChange = Math.round(ship._x-this._x);
  109.                 this._rotation = myDegrees+90;
  110.                 this.shottime--;
  111.                 if (this.shottime == 0) {
  112.                     this.shottime = 40;
  113.                     missle = missles.attachMovie("missle", "missle"+misslecount, missles.getNextHighestDepth(), {_x:this._x, _y:this._y});
  114.                     misslecount++;
  115.                     if (misslecount>15) {
  116.                         misslecount = 0;
  117.                     }
  118.                     if (ship._x>=this._x) {
  119.                         missle.xspeed = (5*(this._x-ship._x)*(this._x-ship._x))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
  120.                     } else {
  121.                         missle.xspeed = 0-(5*(this._x-ship._x)*(this._x-ship._x))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
  122.                     }
  123.                     if (ship._y>=this._y) {
  124.                         missle.yspeed = (5*(this._y-ship._y)*(this._y-ship._y))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
  125.                     } else {
  126.                         missle.yspeed = 0-(5*(this._y-ship._y)*(this._y-ship._y))/(((this._x-ship._x)*(this._x-ship._x))+((this._y-ship._y)*(this._y-ship._y)));
  127.                     }
  128.                     missle.onEnterFrame = function() {
  129.                         this._x += this.xspeed;
  130.                         this._y += this.yspeed;
  131.                         if (this.hitTest(ship)) {
  132.                             ship.gotoAndPlay(2);
  133.                             this.removeMovieClip();
  134.                         }
  135.                         if (this._x<0 or this._x>500 or this._y<0 or this._y>400) {
  136.                             this.removeMovieClip();
  137.                         }
  138.                     };
  139.                 }
  140.                 this._y += motionspeed;
  141.                 if (this._y>400) {
  142.                     this.removeMovieClip();
  143.                 }
  144.             };
  145.             mlcounter++;
  146.         }
  147.     }
  148.     if (mlcounter == 9) {
  149.         mlcounter = 0;
  150.     }
  151. }
  152. function hitenemy() {
  153.     for (i=0; i<9; i++) {
  154.         for (j=0; j<7; j++) {
  155.             if (shotmovie["shot"+j].hitTest(enemies["mlauncher"+i])) {
  156.                 shotmovie["shot"+j].removeMovieClip();
  157.                 enemies["mlauncher"+i].health--;
  158.                 if (enemies["mlauncher"+i].health>0) {
  159.                     enemies["mlauncher"+i].gotoAndPlay(2);
  160.                 } else {
  161.                     for (k=0; k<Math.random()*8; k++) {
  162.                         star = bonusmovie.attachMovie("bonus", "bonus"+bonusmovie.getNextHighestDepth(), bonusmovie.getNextHighestDepth(), {_x:((enemies["mlauncher"+i]._x+Math.random()*20)-10), _y:((enemies["mlauncher"+i]._y+Math.random()*20)-10)});
  163.                         star.xspeed = (Math.random())-0.5;
  164.                         star.yspeed = motionspeed;
  165.                         star.onEnterFrame = function() {
  166.                             this._x += this.xspeed;
  167.                             this._y += this.yspeed;
  168.                             if (this.hitTest(ship)) {
  169.                                 this.removeMovieClip();
  170.                             }
  171.                         };
  172.                     }
  173.                     enemies["mlauncher"+i].removeMovieClip();
  174.                 }
  175.             }
  176.         }
  177.     }
  178. }


At this time I really don’t feel like writing the explanation to the above code. I’ll definitely do it within the next few days. Be sure to check back in on me.

You can have the source code and do what you like with it.

If you make a game with it then be sure to tell me where I can play it. I might even put it up here.

Any questions and comment… feel free to post em.

And here’s the prototype:

You can download the source files here.

Tagged as: , ,

sunil is
Email this author | All posts by sunil

3 Responses »

  1. hi,i saw your tutorials and i really liked the way you covered all the needed basics and explained everything.I cant wait to see more of this game tutorial ,i wanna make a game like this one

  2. Action Scripting is the heart of Flash.

    Animation is just the basic…

    Great game

  3. How do you make the ship die so you can make a game over? I have a menu but need to make the ship get destroyed to make game tougher.

Leave a Reply