0
votes

So in my game I'm using an array of enemies for keeping track of them and updating them and so on - when the player crashes into a enemy its game however, however if the player shoots a bullet that hits the enemy the enemy is deleted. Well thats supposed to be what happens however when the bullet its the enemy it deletes it but for some reason that object is still being updated and can still crash into the player?

How do I go about stopping this?

This is the code that I have in the laser class for checking for collisions

private function loop(e:Event) : void
{
    //move bullet up
    y -= bulletSpeed;

    if (y < 0)
    {
        removeSelf();
    }

    for (var i:int = 0; i < AvoiderGame.enemyArray.length; i++)
    {
        if (hitTestObject(AvoiderGame.enemyArray[i]))
        {
            AvoiderGame.enemyArray[i].takeHit();
            removeSelf();
        }
    }
}

This is all the code that I have in the enemy class.

package 
{

import flash.display.MovieClip;
import flash.display.Stage;

public class Enemy extends MovieClip
{
    private var stageRef:Stage;

    public function Enemy(startX:Number, startY:Number, stageRef:Stage)
    {
        this.stageRef = stageRef;
        x = startX;
        y = startY;
    }       
    public function moveDown():void
    {
        y = y + (1 + (10 - 1) * Math.random());
        switch(Math.round(1 + (2 - 1) * Math.random()))
        {
            case 1: x = x + (1 + (10 - 1) * Math.random());
                    break;
            case 2: x = x - (1 + (10 - 1) * Math.random());
                    break;
        };
    }

    public function StayOnScreen():void
    {
        if  (x <= 0)
        {
            x = 0;
        }
        else if (x >= stageRef.stageWidth)
        {
            x = stageRef.stageWidth;
        }
        else
        {
            x = x;
        }
    }

    private function removeSelf() : void
    {
        if (stageRef.contains(this))
        {
            this.parent.removeChild(this);
            delete AvoiderGame.enemyArray[this];
        }

    }

    public function takeHit() : void
    {
        removeSelf();
    }
    }
}

This is all the code that I have in the main game.

package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;
import flashx.textLayout.formats.BackgroundColor;

public class AvoiderGame extends MovieClip
{
    public static var enemyArray:Array;
    public var enemy:Enemy
    public var Background:gameBackground;

    public var avatar:Avatar;
    public var gameTimer:Timer;

    private var _collisionTest:CollisionTest;
    private var numStars:int = 80;

    private var fireTimer:Timer; //causes delay between fires
    private var canFire:Boolean = true; //can you fire a laser

    public function AvoiderGame()
    {
        Background = new gameBackground();
        addChild(Background);

        enemyArray = new Array();
        var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 2, stage);
        enemyArray.push(enemy);
        addChild(enemy);

        avatar = new Avatar(stage);
        addChild(avatar);

        avatar.x = stage.stageWidth / 2;
        avatar.y = stage.stageHeight / 2;

        for (var i:int = 0; i < numStars; i++)
        {
            stage.addChildAt(new Star(stage), 1);
        }

        _collisionTest = new CollisionTest();

        gameTimer = new Timer(25);
        gameTimer.addEventListener(TimerEvent.TIMER, onTick);
        gameTimer.start();

        fireTimer = new Timer(300, 1);
        fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
        fireTimer.start();

    }

    public function onTick(timerEvent:TimerEvent):void 
    {
        if (Math.random() < 0.1)
        {
            var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
            enemyArray.push(enemy);
            addChild(enemy);
        }

        avatar.UpdateAvatar(canFire);
        if (canFire == true)
        {
            canFire = false;
            fireTimer.start();
        }
        avatar.StayOnScreen();

        for each (var enemy:Enemy in enemyArray)
        {
            enemy.moveDown();
            enemy.StayOnScreen();
            if (_collisionTest.complex(enemy, avatar)) 
            {
                gameTimer.stop();
            }
        }
    }
    private function fireTimerHandler(e:TimerEvent) : void
    {
        //timer ran, we can fire again.
        canFire = true;
    }
}

I understand that im probably going wrong in the enemy class with the removeSelf() function but how would i go about fixing this?

If you dont get want I mean - there is a playable example of this game on my website using space to shot will destroy the enemy but it will still be in the game?

http://dynamite-andy.co.uk/projects/free-time/as3-avoider-game/

1

1 Answers

0
votes

The problem seems to be with this line in the removeSelf() method:

delete AvoiderGame.enemyArray[this];

To delete something from an Array you have to use one the Array class methods that modifies the array rather than the delete keyword.

[EDIT] In this case, you can use splice(), to remove the enemy from the array:

var enemyIndex:int = this.parent.getChildIndex(this);
this.parent.removeChild(this);
AvoiderGame.enemyArray.splice(enemyIndex, 1);