无法访问空对象引用的属性或方法:请帮帮我

时间:2018-07-11 11:29:00

标签: flash adobe cs3

我粘贴了我的编码。最初,我在编写代码时不小心进入了第2帧,但是后来我删除了代码文件并重新创建了文件。但是,问题仍然存在。谁能帮我这个代码

public class firstGame extends MovieClip 
{
    public var mcPlayer:MovieClip;
    private var leftKeyIsDown:Boolean;
    private var rightKeyIsDown:Boolean;

    public function firstGame() 
    {
        //trace("First Game Loaded");
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
        stage.addEventListener(Event.ENTER_FRAME, gameLoop);
    }

    private function gameLoop(e:Event):void 
    {
            trace("Loaded");
    }

    private function playerControl():void
    {
        if (leftKeyIsDown == true)
        {
            mcPlayer.x -= 5;
        }

        if (rightKeyIsDown == true)
        {
            mcPlayer.x += 5;
        }
    }

    private function keyUp(e:KeyboardEvent):void 
    {

        if (e.keyCode == 37)
        {
            //left key released
            leftKeyIsDown = false;
        }
        if (e.keyCode == 39)
        {
            //right key released
            rightKeyIsDown = false;
        }
    }

    private function keyDown(e:KeyboardEvent):void
    {
        if (e.keyCode == 37)
        {
            //left key released
            leftKeyIsDown = true;
        }
        if (e.keyCode == 39)
        {
            //right key released
            rightKeyIsDown = true;
        }
    }



}

错误是

TypeError:错误#1009:无法访问空对象引用的属性或方法。在此处输入代码     在firstGame / playerControl()     在firstGame / gameLoop()

请帮我解决一个问题

1 个答案:

答案 0 :(得分:0)

mcPlayerplayerControl函数中唯一的对象引用,因此mcPlayer必须为null。您需要将mcPlayer分配给播放器动画片段

的实例。