0

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

  • File > Publish Settings > Permit Debugging. That will give you the exact class, or frame and the line number, that produces the error. After narrowing the problem to a single line, please, update your question. Also, add the **actionscript-3** tag because it is a problem between the script and Flash platform. – Organis Jul 12 '18 at 15:28

1 Answers1

0

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

weew
  • 363
  • 3
  • 12