samedi 11 mars 2017

Trying to get AS3 to detect collision but get Error 1009 (Null Object)

I've just only began learning AS3 as I have to create a simple game for an assignment, I have two objects named obj_laser and obj_enemy1 but when I run the collision code I created, it keeps coming up with the error 1009. Here is some of the code and I know its messy.

stage.addEventListener(Event.ENTER_FRAME, runGame);
function runGame(event: Event): void {

//Fire Laser

LASERALARM++;
if (LASERALARM >= 10) {
    var laser: obj_laser = new obj_laser
    var laser2: obj_laser = new obj_laser
    var flare: obj_flare = new obj_flare
    var flare2: obj_flare = new obj_flare
    laser.x = obj_spaceship.x + 12
    laser.y = obj_spaceship.y
    laser2.x = obj_spaceship.x - 12
    laser2.y = obj_spaceship.y
    addChild(laser);
    addChild(laser2);
    flare.x = obj_spaceship.x + 12
    flare.y = obj_spaceship.y
    flare2.x = obj_spaceship.x - 12
    flare2.y = obj_spaceship.y
    addChild(flare);
    addChild(flare2);
    LASERALARM = 0;

//Move Laser

    laser.addEventListener(Event.ENTER_FRAME, moveLaser);
    function moveLaser(event: Event): void {
        laser.y -= 5;
        laser2.y -= 5;
    }
}

//Spawn Enemies

ENEMYALARM++
if (ENEMYALARM >= (randomRange(100, 400))) {
    var enemy1: obj_enemy1 = new obj_enemy1
    enemy1.x = (randomRange(15, 165));
    enemy1.y = -10;
    addChild(enemy1);
    ENEMYALARM = 0;
    //Move Enemy

    enemy1.addEventListener(Event.ENTER_FRAME, moveEnemy);
    function moveEnemy(event: Event): void {
        enemy1.y += 2;
    }
if (laser.hitTestObject(enemy1)) {
    parent.removeChild(laser);
    parent.removeChild(enemy1);
}
}

Aucun commentaire:

Enregistrer un commentaire