从类中访问时间轴对象,给出空对象引用?

时间:2014-12-24 16:05:36

标签: actionscript-3 flash actionscript flash-cs6

所有迟到的人仍然没有达到这个问题的答案,你可能会在下面看到的是一个不错的语法错误,一个很好的成员找到我

错误:

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

当我试图添加具有实例名称world的对象中的实例名称wall0x对象时,我发现我得到了一个空对象错误。 还要忽略那些不相关的长变量列表。

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

        }
}

代码I在第2帧中使用创建播放器,然后将其和弦连续设置为另一个对象。

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);
            }
        }

1 个答案:

答案 0 :(得分:3)

首先:您在这两行中有语法错误:

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

MovieClip之后不应该有一段时间,所以看起来应该是这样的:

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

第二:您始终在每一帧都创建一个新的播放器。在createPlayer方法中,您有以下条件:

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

您没有在该框架范围内定义_player var或createPlayer方法的范围,您已在else语句的范围内定义了它(仅在else语句中提供)

使用其他框架范围的变量将var _player:Player移动到时间轴代码的顶部。

第三次:您试图访问root构造函数中的Player,问题是,当构造函数运行时,您的Player是尚未显示在显示树上,因此在您将播放器添加到舞台之前,root为空。

示例:

_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

更改您的Player课程,使其在尝试访问ADDED_TO_STAGE之前听取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);
    }