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

  • Home
  • SEARCH
  • 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 8864035
In Process

The Archive Base Latest Questions

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

I’m having some problems with trying to remove Event.ADD_TO_STAGE basically in this game ive

  • 0

I’m having some problems with trying to remove Event.ADD_TO_STAGE basically in this game ive developed once the lives is less than or equal to zero the game will end and switch to the game over screen however when I do that it kicks back with this error.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.   
at flash.display::DisplayObjectContainer/removeChild()
at States/changeState()
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

This then turns into..

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Enemy/StayOnScreen()
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

I’m not entirely sure why – could anyone clarify why it does and how I could fix it?

package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;

public class AvoiderGame extends MovieClip
{
    var theCallBackFunction:Function;

    public static var enemyArray:Array;
    public var enemy:Enemy
    public var Background:gameBackground;
    public var Lives:Number = 3;

    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(callBack)
    {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
        theCallBackFunction = callBack;
    }

    private function init(e:Event):void
    {
        Background = new gameBackground();
        addChild(Background);

        enemyArray = new Array();

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

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

        _collisionTest = new CollisionTest();

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

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

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

    public function onTick(timerEvent:TimerEvent):void 
    {

        if (Math.random() < 0.1)
        {
            trace('array length: ', AvoiderGame.enemyArray.length);
            enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
            enemyArray.push(enemy);
            addChild(enemy);
            enemy.gotoAndStop("Enemy" + Math.round(1 + (5 - 1) * Math.random()))
        }

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

        for each (var enemy:Enemy in enemyArray)
        {
            enemy.moveDown();
            enemy.StayOnScreen();
            if (_collisionTest.complex(enemy, avatar)) 
            {
                gameTimer.stop();
                for each (var enemy:Enemy in enemyArray)
                {
                    for(var i:int = 0; i < enemyArray.length; i++)
                    {
                        removeChild(enemyArray[i]);
                        enemyArray.splice(i, 1); //remove the i'th element as i'th element is the enemy containing the ID of hit enemy
                    }
                }
                Lives--;
                trace('lives: ', Lives);
                gameTimer.start();
            }
        }

        if (Lives == 0)
        {
            removeEventListener(Event.ADDED_TO_STAGE, init)
            theCallBackFunction(this, "over");
        }
    }
    private function fireTimerHandler(e:TimerEvent) : void
    {
        //timer ran, we can fire again.
        canFire = true;
    }
}
}
  • 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-14T16:08:14+00:00Added an answer on June 14, 2026 at 4:08 pm

    //use weak references so the listener can be disposed of if necessary

    this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    
    private function init(e:Event):void {
    
    //remove this now as we have added the object to the stage
    this.removeEventListener(Event.ADDED_TO_STAGE, init);
    
    //add a new listener that will fire when the object is removed from stage
    this.addEventListener(Event.REMOVED_FROM_STAGE, reset, false, 0, true);
    

    …

    private function reset(e:Event):void {
    
    //this function is called when the object has been removed from the stage
    this.removeEventListener(Event.REMOVED_FROM_STAGE, reset);
    
    //now make sure all timers and stopped and nulled if required
    //as well as all objects removed from stage and nulled if required. e.g.
    
    fireTimer.stop();
    fireTimer = null;
    
    enemyArray = [];
    enemyArray = null;
    
    enemy
    
    removeChild(Background);
    Background = null;
    
    removeChild(avatar);
    avatar = null;
    
    gameTimer.stop();
    gameTimer = null;
    
    _collisionTest = null;
    
    }
    

    //NB. You are adding stars to the stage and have no way of referencing them to remove them.

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

    //you might want to add them to an array so you can remove them.

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

    Then to remove them:

    for (var i:int = starArray.length; i > 0; i--)
    {
    
       stage.removeChild(starArray[starArray.length-1]);
    
    }
    starArray = [];
    starArray = null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.