cannot access a property or method of a null object reference:help me out

77 Views Asked by At

I have pasted my coding. Initially, I was accidentally in the frame 2 while writing the coding, but then I deleted the code file and recreate the file. But still, the problem persists. Can anyone help me with this code

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



}

and THE ERROR IS

TypeError: Error #1009: Cannot access a property or method of a null object reference. enter code here at firstGame/playerControl() at firstGame/gameLoop()

please help me with a solution

1

There are 1 best solutions below

0
weew On

mcPlayer is the only object reference in the playerControl function, so mcPlayer must be null. You need to assign mcPlayer to the instance of your player movieclip