3

All late comers this question in still active a answer is not yet reached, what you might see below is a irrelevent syntax error a nice member found for me

error:

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

When i'm attempted to add the instance name wall0x objects that are in the object with the instance name world, I find that I get a null object error. Also ignore the long list of variables, not relevant.

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's 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);*/
           }

        }
}

Code I'm using in frame 2 create the player and then continuously set it's chords to another 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);
            }
        }
Trows
  • 391
  • 2
  • 12
  • When Player constructer is called? is it called in frame 2? If your variables are on frame 2 then I think you have to create the Player object in frame 2... – tziuka Dec 24 '14 at 19:00
  • Yea, I am creating a player object in frame 2. It's giving me a syntax error when I add the objects to a list like this though. – Trows Dec 24 '14 at 19:01
  • So, on frame2 of your main timeline, does `world` exist? (and all of it's walls?) or does it only exist on frame 1? – BadFeelingAboutThis Dec 24 '14 at 20:00
  • It only exists on frame 2, the frame in which I'm creating the player – Trows Dec 24 '14 at 20:01
  • Even when I run it like this it throws null object reference errors `else if (_player ==null && world.wall01 != null) { var _player:Player; _player = new Player(); // add to display list stage.addChild(_player); }` – Trows Dec 24 '14 at 20:04
  • Then `world` is probably what is null. Are you able to post your .fla to the cloud to have a look at? Also, when replying to comments, start with @user so they get an alert that you've replied. – BadFeelingAboutThis Dec 24 '14 at 21:42
  • @LDMS http://www.filedropper.com/final_14 – Trows Dec 24 '14 at 22:04

1 Answers1

3

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

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

There should not 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 are always creating a new Player every frame. In your createPlayer method, you have the following conditional:

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 do not have a _player var defined in the scope of that frame or the scope of the createPlayer method, you have defined it inside the scope of the else statement (which makes only available in the else statement)

Move the var _player:Player to the top of your timeline code with the other frame scoped vars.

Third: you are trying to access root in your Player constructor, the problem with this, is that when the contructor runs, your Player is not yet on the display tree, so root is null until you've added player to the stage.

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 your Player class so it listens for being ADDED_TO_STAGE before trying 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);
    }
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Edited that isn't really an issue since that code was a reference from the main timeline I don't think I even needed to root. – Trows Dec 24 '14 at 19:46
  • I don't understand what you're saying? It's a syntax error at any rate and needs to be corrected for your application to run properly (even if it isn't your main issue) – BadFeelingAboutThis Dec 24 '14 at 19:54
  • Wow, thank you. This really helped me understand how classes and objects interact with the timeline. After implementing your suggested fixes there is still a `TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@447ec041 to flash.display.MovieClip. at Player/init() at flash.display::DisplayObjectContainer/addChild() at flash.display::Stage/addChild() at Maintest_fla::MainTimeline/createPlayer() ` Not really sure what this means, I'll reply if I can fix it if you read this and I haven't replied it would be great if you could take a look. – Trows Dec 25 '14 at 02:15
  • When i tried it earlier with your .fla I didn't get any errors. I'm away from my workstation for a few days now though and can't test anything. Looks like somewhere in the `createPlayer` method you must be trying convert the stage to a MovieClip. – BadFeelingAboutThis Dec 25 '14 at 02:58
  • Damnit, ok I'll try it again. – Trows Dec 25 '14 at 03:01