Copy link to clipboard
Copied
hi, i'm trying to make a game in which objects drop down randomly and i got another object to shoot it. problem is everything is working fine until the missile hits the enemy. i want to remove the enemy when it collides with the missile but it's not working. I attempted on using array but i'm not to sure how to work this out since i'm still new. So here's my code :
var myMissile = new Missile();
var myEnemy1 = new Enemy1();
var fallingSpeed1:int;
var fallingSpeed2:int;
var enemyStorage1:int;
var enemyStorage2:int;
function randRange(startNum, endNum) {
return Math.floor(startNum +(Math.random()
* (endNum - startNum)));
}
function doFall1 ():void {
fallingSpeed1 = 3000;
enemyStorage1 = setInterval (createEnemy1, fallingSpeed1);
}
function createEnemy1 ():void {
myEnemy1.y = -50;
myEnemy1.x = Math.random()*stage.stageWidth;
myEnemy1.rotation = randRange(0,360);
myEnemy1.addEventListener(Event.ENTER_FRAME, releaseEnemy1);
addChild(myEnemy1);
}
function releaseEnemy1 (e:Event):void {
var myEnemy1 = Enemy1(e.target);
myEnemy1.y+=5;
if (myEnemy1.hitTestObject(myMissile)){
explode(myEnemy1);
}
}
function explode(myEnemy1):void {
myEnemy1.removeEventListener(Event.ENTER_FRAME, releaseEnemy1);
removeChild(myEnemy1);
}
doFall1();
Copy link to clipboard
Copied
its problematic that you use the same names for local and global variables.
in the beginning you create a defeult enemy named myEnemy1
var myEnemy1 = new Enemy1();
then later on you cast a local var also as myEnemy1
function releaseEnemy1 (e:Event):void {
var myEnemy1 = Enemy1(e.target);
th same name you use later on in another function
function explode(myEnemy1):void {
myEnemy1.removeEventListener(Event.ENTER_FRAME, releaseEnemy1);
removeChild(myEnemy1);
}
1. you should rewrite the explode function like this:
function explode(_enemy:Enemy):void {
_enemy.removeEventListener(Event.ENTER_FRAME, releaseEnemy);
removeChild(_enemy);
}
this is not enough to get your game working, but maybe it gets you on the right track 😉
Find more inspiration, events, and resources on the new Adobe Community
Explore Now