If you found this app helpful, please feel free to donate to our site.
If you found this app helpful, please feel free to donate to our site.
I did this last night for someone who needed the engine fixed up.
If you found this tutorial helpful, please feel free to donate to our site.
Here is the code I did last night for someone that needed an AS2 spaceship game:
/* bullet id/count */
var bcount:Number = 0;
var bulletSpeed:Number = 17;
var currentGun = "lgun";
var firing:Boolean = false;/* leave this alone */
var rofFactor = 10;
/* set rate of fire */
var rof:Number = 8;// don't go over rofFactor (10);
/* we reverse it so we can set a high number for rate of fire. so now if you set the first rof to
a low number it will make firing slower and higher number = higher firing rate */
rof = (rofFactor - rof)+1;
var ftimer:Number = 0;
/* start our levels count*/
var level:Number = 0;
/* create an empty movieclip object to hold our airplane */
var airplane:MovieClip = createEmptyMovieClip("airplane", level);
/* set the airplane in the middle of the screen */
airplane._x = Stage.width / 2;
airplane._y = Stage.height / 2;
level++;
/* attach the body of the airplane to the airplane movieclip object */
airplane.attachMovie("airplane","body",level,{_x:0, _y:0});
level++;
airplane.speed = 5;
airplane._rotation = 0;
/* attach a left gun to our airplane */
airplane.attachMovie("gun","lgun",level,{_x:-22, _y:4});
level++;
airplane.attachMovie("gun","rgun",level,{_x:+22, _y:4});
level++;
/* rotate right gun so it faces forward */
airplane.rgun._rotation += 180;
/* left/right transitions for airplane */
var rightChange:Number = (Stage.width + airplane._width) - 40;
var leftChange:Number = (0 - airplane._width) + 40;
/* top/bottom transitions */
var upChange:Number = (0 - airplane._height) + 20;
var downChange:Number = (Stage.height + airplane._height) - 20;
/* loops */
airplane.onEnterFrame = function() {
if (Key.isDown(37) || Key.isDown(65)) {
this._x -= this.speed;
}
if (Key.isDown(38) || Key.isDown(87)) {
this._y -= this.speed;
}
if (Key.isDown(39) || Key.isDown(68)) {
this._x += this.speed;
}
if (Key.isDown(40) || Key.isDown(83)) {
this._y += this.speed;
}
/* airplane will leave the screen so enter the other side */
if (airplane._x > rightChange) {
airplane._x = leftChange;
}
if (airplane._x < leftChange) {
airplane._x = rightChange;
}
if (airplane._y < upChange) {
airplane._y = downChange;
}
if (airplane._y > downChange) {
airplane._y = upChange;
}
/* loop the bullets */
for (i = 0; i < bcount; i++) {
//trace(_root["b_" + i]._y);
_root["b_" + i]._y -= bulletSpeed;
if (_root["b_" + i]._y < 0) {
removeMovieClip(_root["b_" + i]);
}
}
/* see if they are firing (holding down the mouse)*/
if (firing && ftimer == 0) {
fire();
}
ftimer++;
trace(ftimer);
if (ftimer == rof) {
ftimer = 0;
}
};
function fire() {
attachMovie('bullet','b_' + bcount,_root.getNextHighestDepth());
if (currentGun == "lgun") {
var x = airplane._x - 35;
var y = airplane._y - 7;
currentGun = "rgun";
} else {
var x = airplane._x + 35;
var y = airplane._y - 7;
currentGun = "lgun";
}
_root["b_" + bcount]._x = x;
_root["b_" + bcount]._y = y;
bcount++;
}
/* add mouse and keyboard listener */
var mouseListener:Object = new Object();
mouseListener.onKeyDown = function() {
if (Key.getCode() == 32) {
//fire();
}
};
mouseListener.onMouseDown = function() {
firing = true;
};
mouseListener.onMouseUp = function() {
firing = false;
};
Key.addListener(mouseListener);
Mouse.addListener(mouseListener);
If you found this tutorial helpful, please feel free to donate to our site.