Creating a Flash Shooter Game like SkyForce: Part 3a
By sunil • Sep 4th, 2008 • Category: TutorialsSo 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.
-
stop();
-
xspeed = 0;
-
yspeed = 0;
-
friction = 0.8;
-
turntest = 0;
-
shotcount = 0;
-
shottime = 10;
-
motionspeed = 1;
-
xmoved = 0;
-
mlcounter = 0;
-
misslecount = 0;
-
xdist = new Array(20, 250, 260, 550, 560, 570, 850, 860, 870, 880, 1250, 1260, 1270, 1280, 1290, 1550, 1560);
-
xplace = new Array(100, 410, 110, 380, 220, 125, 260, 300, 110, 350, 155, 300, 205, 65, 370, 105, 210);
-
_root.attachMovie("back1", "back1", _root.getNextHighestDepth(), {_x:250, _y:0});
-
_root.attachMovie("back2", "back2", _root.getNextHighestDepth(), {_x:250, _y:-400});
-
_root.createEmptyMovieClip("shotmovie", _root.getNextHighestDepth());
-
_root.createEmptyMovieClip("enemies", _root.getNextHighestDepth());
-
_root.createEmptyMovieClip("bonusmovie", _root.getNextHighestDepth());
-
_root.createEmptyMovieClip("missles", _root.getNextHighestDepth());
-
_root.attachMovie("ship", "ship", _root.getNextHighestDepth(), {_x:250, _y:400});
-
ship.onEnterFrame = function() {
-
_root.shipmover();
-
if (xmoved>1600) {
-
xmoved = 0;
-
}
-
_root.shooter();
-
_root.mover();
-
_root.renderer();
-
_root.hitenemy();
-
};
-
function shipmover() {
-
ship._x += xspeed;
-
ship._y += yspeed;
-
if (ship._x<10) {
-
ship._x = 10;
-
}
-
if (ship._x>490) {
-
ship._x = 490;
-
}
-
if (ship._y<40) {
-
ship._y = 40;
-
}
-
if (ship._y>390) {
-
ship._y = 390;
-
}
-
xspeed = xspeed*friction;
-
yspeed = yspeed*friction;
-
if (Key.isDown(Key.UP)) {
-
yspeed--;
-
}
-
if (Key.isDown(Key.DOWN)) {
-
yspeed++;
-
}
-
if (Key.isDown(Key.LEFT)) {
-
turntest++;
-
xspeed--;
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
turntest--;
-
xspeed++;
-
}
-
if (turntest != 0) {
-
ship._width = 35;
-
} else {
-
ship._width = 40;
-
}
-
turntest = 0;
-
}
-
function mover() {
-
back1._y += motionspeed;
-
back2._y += motionspeed;
-
if (back1._y>=400) {
-
back1._y = -400;
-
}
-
if (back2._y>=400) {
-
back2._y = -400;
-
}
-
xmoved++;
-
}
-
function shooter() {
-
shottime--;
-
if (shottime == 0) {
-
if (shotcount == 7) {
-
shotcount = 0;
-
}
-
shottime = 10;
-
shot = shotmovie.attachMovie("shot", "shot"+shotcount, shotmovie.getNextHighestDepth(), {_x:ship._x, _y:ship._y-14});
-
shotcount++;
-
shot.speed = 8;
-
shot.onEnterFrame = function() {
-
this._y -= this.speed;
-
if (this._y<0) {
-
this.removeMovieClip();
-
}
-
};
-
}
-
}
-
function renderer() {
-
for (i=0; i<17; i++) {
-
if (xmoved == xdist[i]) {
-
enemy = enemies.attachMovie("mlauncher", "mlauncher"+mlcounter, enemies.getNextHighestDepth(), {_x:xplace[i], _y:0});
-
enemy.health = 5;
-
enemy.shottime = 20;
-
enemy.onEnterFrame = function() {
-
myRadians = Math.atan2(ship._y-this._y, ship._x-this._x);
-
myDegrees = Math.round((myRadians*180/Math.PI));
-
_root.yChange = Math.round(ship._y-this._y);
-
_root.xChange = Math.round(ship._x-this._x);
-
this._rotation = myDegrees+90;
-
this.shottime--;
-
if (this.shottime == 0) {
-
this.shottime = 40;
-
missle = missles.attachMovie("missle", "missle"+misslecount, missles.getNextHighestDepth(), {_x:this._x, _y:this._y});
-
misslecount++;
-
if (misslecount>15) {
-
misslecount = 0;
-
}
-
if (ship._x>=this._x) {
-
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)));
-
} else {
-
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)));
-
}
-
if (ship._y>=this._y) {
-
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)));
-
} else {
-
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)));
-
}
-
missle.onEnterFrame = function() {
-
this._x += this.xspeed;
-
this._y += this.yspeed;
-
if (this.hitTest(ship)) {
-
ship.gotoAndPlay(2);
-
this.removeMovieClip();
-
}
-
if (this._x<0 or this._x>500 or this._y<0 or this._y>400) {
-
this.removeMovieClip();
-
}
-
};
-
}
-
this._y += motionspeed;
-
if (this._y>400) {
-
this.removeMovieClip();
-
}
-
};
-
mlcounter++;
-
}
-
}
-
if (mlcounter == 9) {
-
mlcounter = 0;
-
}
-
}
-
function hitenemy() {
-
for (i=0; i<9; i++) {
-
for (j=0; j<7; j++) {
-
if (shotmovie["shot"+j].hitTest(enemies["mlauncher"+i])) {
-
shotmovie["shot"+j].removeMovieClip();
-
enemies["mlauncher"+i].health--;
-
if (enemies["mlauncher"+i].health>0) {
-
enemies["mlauncher"+i].gotoAndPlay(2);
-
} else {
-
for (k=0; k<Math.random()*8; k++) {
-
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)});
-
star.xspeed = (Math.random())-0.5;
-
star.yspeed = motionspeed;
-
star.onEnterFrame = function() {
-
this._x += this.xspeed;
-
this._y += this.yspeed;
-
if (this.hitTest(ship)) {
-
this.removeMovieClip();
-
}
-
};
-
}
-
enemies["mlauncher"+i].removeMovieClip();
-
}
-
}
-
}
-
}
-
}
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:
If you liked this post, Buy me some beer (or coffee)sunil is
Email this author | All posts by sunil

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
Action Scripting is the heart of Flash.
Animation is just the basic…
Great game
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.
Hi, I am having trouble putting all the scripting together, would it be possible that you could fix the dl on the “explained” page, I believe it would be able to help me understand it better. Also, I thought that it was very well putting together. Looking forward to future tutorials
Thanks for telling me about the broken link,
Anyways that link would have redirected you here, the complete prototype is linked at the bottom of this post below the swf
For your convenience here it is http://www.itsnags.com/shottutpart3/shooter.fla
hmm, the fla download link that your provided for me doesn’t seem to work, when i click on it, it brings up a bunch of random code… would the problem be because I have a mac? thanks ÐÏࡱá
Well it may be because I am experiencing some downtime on the servers. If you wish I could email it to you.
If you could email to me, I would appreciate it very much. Thank you. Looking forward to more tutorials.
here is my email, dc_cyclone@yahoo.com
Hey man, cool tutorial, I noticed that this is 3a, so does that mean that you’re ganna do more with health and death etc.
Hey great Tutorial.
It has been a great help.
I may be only fourteen but i have made my own version.
http://www.knowlesy.co.uk/game.html
lines 105, 106 and 109 could be simply something like
turret._rotation=Math.atan2(player._y-turret._y,player._x-turret._x)*57.2;
“*pi/180″ is equel to “*57.2″,
Less work for flash means a lot faster [=.
Also, less variables is always better. Unless, you have upgrades,
Also, there are some more things to speed up/make more compact this code.
if(Key.isDown(Key.LEFT)) {
//blah blah
} else if (Key.isDown(Key.RIGHT)) {
//blah blub
}
Else works faster becouse flash will skip those tests if the if statement is true.
And just out of curiosity, why use onEnterFrame 5 times? I’m sorry but I think those code can be shorten to a hundred lines.