Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:13:09+00:00 2026-06-14T12:13:09+00:00

So in my game I’m using an array of enemies for keeping track of

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:13:10+00:00Added an answer on June 14, 2026 at 12:13 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a game using a 10x10 2D array. The player starts at
I have an online game currently using MySQL. I have a Player table looking
My game is using Cocos2d-x. When a sound plays, the game crashes randomly on
My game is a small shooting game in cocos2d. The enemy generates the bullets
I'm designing a game for windows 8 using HTML5 , but my game can't
The game will be written in C++ Programming: enemies.puch_back(new DefaultEnemy(200, 300, 3, 5)); enemies.puch_back(new
I have a game which displays an array of colored blocks. The user can
I am porting a game using SpiderMonkey to Android. Because I need to integrate
I am developing a Game and I didn't want an array full of levels
I have a game where each player (A total of 5 players play the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.