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:
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.