samedi 25 février 2017

What to do with the error " Cannot access a property or method of a null object reference."?

I've been getting this error since I added the code for my timer. It looks like I can't switch frames or something. Or maybe I have to clear something before moving to another frame. This is from a game I created and now I've decided to make speed as the basis of scoring. The higher the score the sooner you finish a level. Please help.

stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN,Directions);
stage.addEventListener(Event.ENTER_FRAME,Kikoy);
NextOne_btn.addEventListener(MouseEvent.CLICK,NextLevelOne);
Ok_btn.addEventListener(MouseEvent.CLICK,DoneHow);
var hasBlueKeyOne:Boolean = false;
levelCompleteOne_mc.visible =false;
NextOne_btn.visible =false;
back.addEventListener(MouseEvent.CLICK, PlayMen1);

function PlayMen1(event:MouseEvent):void
{
    gotoAndPlay("StartMenu");
}

pawup.addEventListener(MouseEvent.CLICK,pawUp);
pawdown.addEventListener(MouseEvent.CLICK,pawDown);
pawleft.addEventListener(MouseEvent.CLICK,pawLeft);
pawright.addEventListener(MouseEvent.CLICK,pawRight);

function pawLeft(event:MouseEvent):void
{
    if(BlueKeyOne_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y, true))
    {BlueKeyOne_mc.x=850;BlueKeyOne_mc.y=45;BlueKeyOne_mc.scaleX+=0.1; BlueKeyOne_mc.scaleY+=0.1;
    hasBlueKeyOne=true;}
    if(!Blocks_mc.hitTestPoint(Avatar_mc.x-35, Avatar_mc.y, true)){Avatar_mc.x-=40;}
}
function pawUp(event:MouseEvent):void
{
    if(BlueKeyOne_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y, true))
    {BlueKeyOne_mc.x=850;BlueKeyOne_mc.y=45;BlueKeyOne_mc.scaleX+=0.1; BlueKeyOne_mc.scaleY+=0.1;
    hasBlueKeyOne=true;}
    if(!Blocks_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y-35, true))Avatar_mc.y-=40;
}

function pawRight(event:MouseEvent):void
{
    if(BlueKeyOne_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y, true))
    {BlueKeyOne_mc.x=850;BlueKeyOne_mc.y=45;BlueKeyOne_mc.scaleX+=0.1; BlueKeyOne_mc.scaleY+=0.1;
    hasBlueKeyOne=true;}
    if(!Blocks_mc.hitTestPoint(Avatar_mc.x+35, Avatar_mc.y, true))Avatar_mc.x+=40;
}

function pawDown(event:MouseEvent):void
{
    if(BlueKeyOne_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y, true))
    {BlueKeyOne_mc.x=850;BlueKeyOne_mc.y=45;BlueKeyOne_mc.scaleX+=0.1; BlueKeyOne_mc.scaleY+=0.1;
    hasBlueKeyOne=true;}
    if(!Blocks_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y+35, true))Avatar_mc.y+=40;
}



function Directions(event:KeyboardEvent):void
{
    if(BlueKeyOne_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y, true))
    {BlueKeyOne_mc.x=850;BlueKeyOne_mc.y=45;BlueKeyOne_mc.scaleX+=0.1; BlueKeyOne_mc.scaleY+=0.1;
    hasBlueKeyOne=true;}

    if(event.keyCode==37){
        if(!Blocks_mc.hitTestPoint(Avatar_mc.x-35, Avatar_mc.y, true)){Avatar_mc.x-=40;}
    }
    if(event.keyCode==38){
        if(!Blocks_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y-35, true))Avatar_mc.y-=40;
    }

    if(event.keyCode==39){
        if(!Blocks_mc.hitTestPoint(Avatar_mc.x+35, Avatar_mc.y, true))Avatar_mc.x+=40;
    }

    if(event.keyCode==40){
        if(!Blocks_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y+35, true))Avatar_mc.y+=40;
    }

}


function Kikoy(event:Event):void
    {

        if(Door_mc.hitTestPoint(Avatar_mc.x, Avatar_mc.y, true) && hasBlueKeyOne){
            levelCompleteOne_mc.visible =true;
            BlueKeyOne_mc.visible=false;
            NextOne_btn.visible =true;
            stage.removeEventListener(Event.ENTER_FRAME, Kikoy);
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, Directions);
        }
    }


function NextLevelOne(Event:MouseEvent):void
{
    WhatLevel=2;
    gotoAndStop("ToIrosin");
}

function DoneHow(Event:MouseEvent):void
{
    Ok_btn.visible=false;
    Instruction_mc.visible=false;
}



GameOverOne_mc.visible = false;
tryAgainOne_btn.visible=false;
timerFunction(1,30);

function timerFunction(minutes, seconds)
{
    var seconds = seconds;
    var minutes = minutes;
    var clock;
    var tmr = setInterval(timer,100);

    function timer()
    {
        seconds--;
        if (seconds<0)
        {
            minutes--;
            seconds = 59;
        }
        if (minutes == 0 && seconds == 0)
        {
            clearInterval(tmr);
            GameOverOne_mc.visible = true;
            tryAgainOne_btn.visible=true;
        }
        clock = minutes + "0" + seconds;
        if (seconds<10)
        {
            if (minutes<10)
            {
                clock = "0" + minutes + ":0" + seconds;
            }
        }
        else
        {
            if (minutes<10)
            {
                clock = "0" + minutes + ":" + seconds;
            }
            else
            {
                clock = minutes + ":" + seconds;
            }
        }
        txt.embedFonts = false;
        txt.text = clock;

    }

}

Aucun commentaire:

Enregistrer un commentaire