Accessing timeline objects from a class giving a reference to a null object?

Everyone else in this question in the yet active answer has yet to reach, what you can see below is an inappropriate syntax error found for me by a good member

Mistake:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Player()
    at Maintest_fla::MainTimeline/createPlayer()

      

When I try to add the object name wall0x of the instance name that is in the object with the instance name world, I found myself getting a null object error. Also ignore the long list of variables that don't matter.

package 
{

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.filters.BlurFilter;
    import flash.utils.Timer;



    public class Player extends MovieClip
    {
        // player settings



        private var _rotateSpeedMax:Number = 20;
        public var _gravity:Number = .10;
        // projectile gun settings
        public var _bulletSpeed:Number = 4;
        public var _maxDistance:Number = 200;
        public var _reloadSpeed:Number = 250;//milliseconds
        public var _barrelLength:Number = 20;
        public var _bulletSpread:Number = 5;
        // gun stuff
        private var _isLoaded:Boolean = true;
        private var _isFiring:Boolean = false;
        private var _endX:Number;
        private var _endY:Number;
        private var _startX:Number;
        private var _startY:Number;
        private var _reloadTimer:Timer;
        private var _bullets:Array = [];

        // array that holds walls

        public var _solidObjects:Array = [];

        //
        private var _player:MovieClip;
        private var _dx:Number;
        private var _dy:Number;
        private var _pcos:Number;
        private var _psin:Number;
        public var _trueRotation:Number;



        public function Player()
        {

            // constructor code   //Right hereVVVthe instance name is wall0x and it in the object world on the stage.                            
                _solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];


            /*addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
            addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);*/
           }

        }
}

      

The code I'm using in frame 2 is creating a player and then continuously setting it to other objects.

stage.addEventListener(Event.ENTER_FRAME, createPlayer);
function createPlayer(e:Event):void
        {


            // attach player movieclip from library

            // position player in center
            if (character!=null&&_player!=null)
            {

                _player.x = character.x + 5;
                _player.y = character.y + 5;
            }
            else if (_player ==null && world.wall01 != null)
            {
            var _player:Player;


                _player = new Player();

                // add to display list
                stage.addChild(_player);
            }
        }

      

+3


source to share


1 answer


First: You have a syntax error on these two lines:

_player.x = MovieClip.(root).character.x + 5;
_player.y = MovieClip.(root).character.y + 5;

      

There shouldn't be a period after MovieClip

, so it should look like this:

_player.x = MovieClip(root).character.x + 5;
_player.y = MovieClip(root).character.y + 5;

      

Second . You always create a new Player for every frame. In your method createPlayer

, you have the following condition:

if(character != null && _player != null)  //_player is not a defined in this scope, so it will either throw an error, or always return null/undefined

      

You don't have a _player

var parameter defined in the scope of this frame or method scope createPlayer

, you defined it inside the scope of a statement else

(which only makes it available in the else statement)



Move var _player:Player

to the top of your timeline code using other scoped frames.

Third : you are trying to access root

in your constructor Player

, the problem with that is that when you run the constructor, yours Player

is not yet visible in the display tree, so root is null until you add the player to the scene.

Example:

_player = new Player(); //this will run your contructor, but root will be null
stage.addChild(_player); //after this, your Player class will now have access to root/stage/parent object

      

Change the class Player

to listen ADDED_TO_STAGE

before trying to access root

.

    public function Player()
    {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
        // constructor code

    }

    private function init(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, init);
        _solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
        addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
    }

      

+3


source







All Articles