1 Replies - 1941 Views - Last Post: 30 January 2011 - 03:34 AM

#1 pmiller624  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 02-May 10

Error removing child and removing event listener (AS3)

Posted 27 January 2011 - 10:30 PM

I'm trying to build a small game to try and get into ActionScript. This is my first attempt basically at ActionScript, so go easy :).

Currently I have an Enemy superclass and an Enemy_One class that extends that superclass.

When an enemy has moved off screen I want to remove it from the frame.

Here's the errors I'm getting:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Enemy/checkHitPlayer()
    at Enemy/loop()


Enemy.as
package{
    
    import flash.display.MovieClip;
    import flash.events.*;
    
    public class Enemy extends MovieClip{

        public function Enemy(){
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        public function loop(event:Event):void
        {
            moveEnemy();
            checkOnScreen();
            checkHitBullet();
            checkHitPlayer();
        }

        public function moveEnemy(){}
        
        public function remove():void
        {
            removeEventListener(Event.ENTER_FRAME, loop);
            parent.removeChild(this);
        }
        
        public function checkOnScreen():void
        {
            if(y > stage.height+5)
            {
                remove();
            }
        }
        
        public function checkHitBullet():void
        {
            
        }
        
        public function checkHitPlayer():void
        {
            // hit testing against the user
            if(hitTestObject(MovieClip(root).mcPlayer))
            {
                trace('Player has been hit by enemy');
            }    
        }

    }
    
}



Enemy_One.as
package{
    
    import flash.events.*;
    
    public class Enemy_One extends Enemy
    {
        
        // speed of enemy
        var maxSpeed:int = 5;

        public function Enemy_One()
        {
            
        }
        
        override public function moveEnemy(){
            y += maxSpeed;
        }

    }
    
}


This post has been edited by pmiller624: 28 January 2011 - 09:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error removing child and removing event listener (AS3)

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Error removing child and removing event listener (AS3)

Posted 30 January 2011 - 03:34 AM

It is most probably happening bcoz you are trying to access a reference to a null object (object that you have already removed).
Its happening in checkHitPlayer(). Check MovieClip(root).mcPlayer before using it.

Also you might be accessing a reference to an Enemy which is null (removed).

This post has been edited by chinchang: 30 January 2011 - 03:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1