I wrote a small game in AS 2.
You just have to shoot at some alien ships. All works fine but I want to add one thing though.
I want to make the gun disappear if it hits any of the alien ships.
I tried to include a hitTest on the mcGun.onEnterFrame event and to remove it from there but I get an error ( not in the output window): when I shoot a bullet the gun disappears…I just can’t understand why :((
Here’s the code for the game:
/*****declare variables and instances ******/
var gunSpeed:Number = 5;
var bulletCount:Number = 1;
var bulletSpeed:Number = 10;
var alienCount:Number = 1;
var alienSpeed:Number = 5;
var hits:Number = 0;
var misses:Number = 0;
/************create objects************/
var keyListener:Object = new Object();
var initBullet:Object = new Object();
var initAlien:Object = new Object();
var sndShoot:Sound = new Sound();
sndShoot.attachSound("shoot");
var sndExplosion:Sound = new Sound();
sndExplosion.attachSound("explosion");
/**********handle events**************/
initAlien.onEnterFrame = function() {
this._y += alienSpeed;
if (this._y>Stage.height) {
removeMovieClip(this);
misses++;
txtMisses.text = misses;
if (misses == 5) {
mcGameOver._visible = true;
clearInterval(game);
}
}
};
initBullet.onEnterFrame = function() {
this._y -= bulletSpeed;
for (var i:Number = 1; i<=alienCount; i++) {
if (this.hitTest(this._parent["mcAlien"+i])) {
//removeMovieClip(this._parent["mcAlien"+i]);
this._parent["mcAlien"+i].gotoAndPlay(2);
removeMovieClip(this);
sndExplosion.start();
hits++;
txtHits.text = hits;
}
}
if (this._y<0) {
removeMovieClip(this);
}
};
keyListener.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
newBullet();
sndShoot.start();
}
};
mcGun.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
if (this._x<Stage.width-this._width) {
this._x += gunSpeed;
}
}
if (Key.isDown(Key.LEFT)) {
if (this._x>0) {
this._x -= gunSpeed;
}
}
};
/*************functions***************/
function newBullet() {
var bulletName = "mcBullet"+String(bulletCount);
var bulletDepth = this.getNextHighestDepth();
this.attachMovie("bullet", bulletName, bulletDepth, initBullet);
this[bulletName]._x = mcGun._x+mcGun._width/2;
this[bulletName]._y = mcGun._y;
bulletCount++;
}
function newAlien() {
var alienName = "mcAlien"+String(alienCount);
var alienDepth = this.getNextHighestDepth();
this.attachMovie("alien", alienName, alienDepth, initAlien);
this[alienName]._x = Math.random()*(Stage.width-this[alienName]._width)+this[alienName]._width/2;
this[alienName]._y = -50;
alienCount++;
}
/**************run now***************/
Key.addListener(keyListener);
game = setInterval(this, "newAlien", 1000);
mcGameOver._visible = false;
So the above code works fine. But when I add a for loop inside of the mcGun.onEnterFrame the something goes wrong.
mcGun.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
if (this._x<Stage.width-this._width) {
this._x += gunSpeed;
}
}
if (Key.isDown(Key.LEFT)) {
if (this._x>0) {
this._x -= gunSpeed;
}
[COLOR=Green]
[/COLOR][COLOR=Green]**for (var i:Number = 1; i<=alienCount; i++) {
if (this.hitTest(this._parent["mcAlien"+i])) {
removeMovieClip(this);
}
}
}
**[/COLOR]
I wanted to check with the above highlighted code whether the gun hits any of the alien ships. And if it does, the gun should disappear from the stage.
From what I’ve read I cannot remove the gun because it’s a timeline placed movieclip ( its depth is less than 0 ) so it cannot be removed dynamically. Correct me here if I’m wrong though I don’t think so.
I tried another approach ( since I’m not familiar with swapDepth—i’ve just started with ActionScript ). I tried to make the gun become not visible if it hits any of the alien ships. But I get the same error. If I shoot a bullet and that bullet hits a ship the gun becomes invisible but keep in mind that the gun has not yet hit any of the ships.
I’m sorry for the LOOOOOONG post but I’m just a beginner and it’s very important for me to clear any doubt I have and to build a good foundation regarding my AS knowledge. Plus it’s so frustrating not understanding what happens
Thank you a lot for your time
Ella