mercredi 27 mai 2015

I keep getting 'ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller' inside timer event

I'm trying to remove this item from the stage and keep getting this error. The code seems to work and the object is removed but the game seems kinda buggy.

Here's the full error: "ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at JumpingGame/onTick2()[E:\Folder\Folder\Folder\Folder\JumpingGame.as:95] at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()"

The error is pointing to:

  • removeChild(enemy)
  • removeChild(leg);
  • removeChild(life);

I'm also getting: "TypeError: Error #1009: Cannot access a property or method of a null object reference" on this line theLives.text = liveLives.toString();

Here's my code:

    package 
{
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class JumpingGame extends MovieClip 
    {
        public var army:Array;
        public var powerups:Array;
        public var pluslives:Array;

        public var newDoodle:doodle;

        public var enemyTimer:Timer;
        public var legTimer:Timer;
        public var lifeTimer:Timer; 

        public function JumpingGame() 
        {

            enemyTimer = new Timer( 40 );
            enemyTimer.addEventListener( TimerEvent.TIMER, onTick );
            enemyTimer.start();

            legTimer = new Timer( 20 );
            legTimer.addEventListener( TimerEvent.TIMER, onTick2 );
            legTimer.start();

            lifeTimer = new Timer( 30 );
            lifeTimer.addEventListener( TimerEvent.TIMER, onTick3 );
            lifeTimer.start();

            army = new Array();
            var newEnemy = new Enemy( 100, -15 );
            army.push( newEnemy );
            addChild( newEnemy );

            powerups = new Array();
            var newLeg = new Leg( 300, -15 );
            powerups.push( newLeg );
            addChild( newLeg );

            pluslives = new Array();
            var newLife = new Plus1( 300, -15 );
            pluslives.push( newLife );
            addChild( newLife );

            newDoodle = new doodle();
            addChild( newDoodle );
            newDoodle.y = stage.stageHeight - 50;
            newDoodle.x = stage.stageWidth / 2;
        }

        public function onTick( timerEvent:TimerEvent ):void
        {
            if (Math.random() <0.005 )
            {
                var randomX:Number = Math.random() * 320;
                var newEnemy:Enemy = new Enemy( randomX, -15 );
                army.push( newEnemy );
                addChild( newEnemy );
            }

            for each ( var enemy:Enemy in army )
            {
                enemy.moveDownABit();
                if (newDoodle.hitTestObject( enemy ) )
                {
                    //gotoAndPlay ("Game Over");
                    removeChild(enemy);
                    liveLives -= 1;
                    theLives.text = liveLives.toString();

                }
            }
        }

        public function onTick2( timerEvent:TimerEvent ):void
        {
            if (Math.random() <0.008 )
            {
                var randomX2:Number = Math.random() * 320;
                var newLeg:Leg = new Leg( randomX2, -15 );
                powerups.push( newLeg );
                addChild( newLeg );
            }

            for each ( var leg:Leg in powerups )
            {
                leg.moveDownABit2();
                if (newDoodle.hitTestObject( leg ) )
                {
                    //gotoAndPlay ("Game Over");
                    removeChild(leg);
                    liveScore += 2000;
                    theScore.text = liveScore.toString();
                }
            }

        }

        public function onTick3( timerEvent:TimerEvent ):void
        {
            if (Math.random() <0.001 )
            {
                var randomX3:Number = Math.random() * 320;
                var newLife:Plus1 = new Plus1( randomX3, -15 );
                pluslives.push( newLife );
                addChild( newLife );
            }

            for each ( var life:Plus1 in pluslives )
            {
                life.moveDownABit3();
                if (newDoodle.hitTestObject( life ) )
                {
                    //gotoAndPlay ("Game Over");
                    removeChild(life);
                    liveLives += 1;
                    theLives.text = liveLives.toString();

                }
            }

        }
    }
}

Aucun commentaire:

Enregistrer un commentaire