Question

I am making a game in flash and my only problem so far is this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at farmgame227_fla::MainTimeline/onenter()[farmgame227_fla.MainTimeline::frame2:144]

The line will vary depending on how I move to the next frame. I am 99% sure a null object is not the problem, so I am stumped. I will show my entire code for frame 2 and I would appreciate it so much if anyone could lend some insight.

stop();

import flash.events.Event;


addEventListener(Event.ENTER_FRAME,onenter);

stop();

//starting positions for sheep
sheep1.y = 330
sheep1.x = 350

sheep2.y = 360
sheep2.x = 380

sheep3.y = 300
sheep3.x = 450

sheep4.y = 330
sheep4.x = 350

gold.y = 150
gold.x = 800

goat.y = 620
goat.x = 800

var myscore = 0;


function onenter(e:Event)
{

    //hand follows mouse cursor
    hand.x = mouseX;
    hand.y = mouseY;

    //sheep velocity
    sheep1.x = sheep1.x + 1;
    sheep1.y = sheep1.y - 1;

    sheep2.x = sheep2.x - 1;
    sheep2.y = sheep2.y - 1;

    sheep3.x = sheep3.x + 1;
    sheep3.y = sheep3.y + 1;

    sheep4.x = sheep4.x - 3;
    sheep4.y = sheep4.y + 3;

    //sheep1's boundaries
    if (sheep1.y < 0 || sheep1.y > 775 || sheep1.x < 0 || sheep1.x > 775)
    {       
        removeEventListener(Event.ENTER_FRAME,onenter);
        nextFrame();
    }

    //sheep2's boundaries
    if (sheep2.y < 0 || sheep2.y > 775 || sheep2.x < 0 || sheep2.x > 775)
    {       
        removeEventListener(Event.ENTER_FRAME,onenter);
        nextFrame();
    }

    //sheep3's boundaries
    if (sheep3.y < 0 || sheep3.y > 775 || sheep3.x < 0 || sheep3.x > 775)
    {       
        removeEventListener(Event.ENTER_FRAME,onenter);
        nextFrame();
    }

    //sheep4's boundaries
    if(sheep4==null)
    {
        trace("4 it's null");
    }
    if (sheep4.y < 0 || sheep4.y > 775 || sheep4.x < 0 || sheep4.x > 775)
    {       
        removeEventListener(Event.ENTER_FRAME,onenter);
        nextFrame();
    }

    //hand hitting sheep, sending them back
    if(sheep1==null)
    {
        trace("1 is null");
    }
    if(hand==null)
    {
        trace("hand is null");
    }
    if(fence==null)
    {
        trace("fence is null");
    }
    if (hand.hitTestObject (sheep1))
    {
        sheep1.y = 330;
        sheep1.x = 350;
        myscore = myscore + 1;
    }
    if (hand.hitTestObject (sheep2))
    {
        sheep2.y = 360;
        sheep2.x = 380;
        myscore = myscore + 1;
    }
    if (hand.hitTestObject (sheep3))
    {
        sheep3.y = 300;
        sheep3.x = 450;
        myscore = myscore + 1;
    }
    if (hand.hitTestObject (sheep4))
    {
        sheep4.y = 330;
        sheep4.x = 350;
        myscore = myscore + 1;
    }

    if (hand.hitTestObject (gold))
    {
        gold.x = 2500;
        gold.y = 610;
        gold.x = gold.x - 9;
        myscore = myscore + 5;
    }

    if (hand.hitTestObject (goat))
    {       
        removeEventListener(Event.ENTER_FRAME,onenter);
        nextFrame();
    }


    //hand touching fence, results in game over
    if (hand.hitTestObject (fence))
    {       
        removeEventListener(Event.ENTER_FRAME,onenter);
        nextFrame();
    }

    score.text = myscore;

    //changing sheep speed based on score
    //score 40 and 55 should be next
    if (myscore >= 12)
    {
        sheep2.x = sheep2.x - 2;
        sheep2.y = sheep2.y - 2;
    }

    if (myscore >= 24)
    {
        sheep3.x = sheep3.x + 2;
        sheep3.y = sheep3.y + 2;
    }

    if (myscore >= 30)
    {
        sheep1.x = sheep1.x + 3;
        sheep1.y = sheep1.y - 3;
    }

    //initiating and controlling gold and goat
    if (myscore > 17)
    {
        gold.x = gold.x - 8;
    }

    if (gold.x < -9)
    {
        gold.x = 2000;
        gold.y = 610;
        gold.x = gold.x - 9;
    }

    if (myscore > 8)
    {
        goat.x = goat.x - 3;
    }

    if (goat.x < -9)
    {
        goat.x = 1600;
        goat.y = 150;
        goat.x = goat.x - 4;
    }

}
Était-ce utile?

La solution

You should shift all the checks in onenter upwards, prior to assigning anything to either sheep or other objects. Then you will likely find that one of the objects is null. The #1009 error means that some object reference that's placed right before a "." qualifier is null, and you can't get properties out of nothing.

Also, try placing return right after any call of nextFrame(), because when you perform nextframe, the previous frame's vars are invalidated, thus you can't refer to say hand because there's no more hand, it's destroyed along with the whole previous frame.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top