TypeError: Error #1009 - AS3
I'm getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Lemonade_fla::MainTimeline/frame3()[Lemonade_fla.MainTimeline::frame3:48]
From this snippet of code:
buy_sugar.addEventListener(MouseEvent.CLICK, buysugar);
function buysugar(event:MouseEvent):void{
if (cash1>=0.50){
buy.play();
cash1 = cash.text.text;
cash1 = cash1 - 0.25;
sugar = sugar_count.text;
sugar=sugar+16;
}
}
开发者_StackOverflow社区
Does anyone have any ideas of why?
Judging by the commentary in the opening question, it seems that buy_sugar is null. You believe that buy_sugar is a movieclip on the timeline, and thus can be safely referenced in your code, but if buy_sugar really existed you wouldn't get this error.
Are you absolutely positive that buy_sugar exists on the same frame as the call you're making?
Let's say you're currently on frame 3...
buy_sugar.addEventListener(Event.FOO, onFoo); // if buy_sugar exists on frame 3, this works.
But what if you're on frame 2?
gotoAndStop(3);
buy_sugar.addEventListener(Event.FOO, onFoo); // problem... this can fail if buy_sugar exists on 3 but NOT on 2.
Make sure that the clip you're referencing actually exists on the frame in which the call is written, and double-check your instance name spelling.
EDIT::
Those pictures are pretty interesting. It does appear that buy_sugar is a movieclip symbol that exists on frame 3, yet buy_sugar traces as null in your frame 3 script. Here are a few suggestions for further debugging.
In the code where you have trace(buy_sugar);, replace it with trace("testing buy_sugar presence ", buy_sugar, currentFrame);
CUT the buy_sugar movieclip from its place on the stage. Make a new layer below all the others, and paste buy_sugar into the keyframe on frame 1. Don't add ANY other keyframes on that layer. The goal is to make sure the buy_sugar exists at all times, on all frames.
Let me know what you see when you do this.
EDIT
Now that we've determined that the clip can be detected when it exists on all frames, scoot that keyframe over from 1 to 3 so that the clip exists only on frame 3. Does it still work? If so, just move that layer around to where it will look nicest.
If it does not work, again move that layer up to where the movieclip will look nicest. Instead of putting the clip only on frame 3, make a keyframe on frame 3 with the clip where you want it, and on all other frames have the clip live somewhere off the visible stage. That's kind of hacky, but it works to solve the problem.
One of the items is not yet defined (or privately defined somewhere else).
Assuming this is the right code snippet, it's either buy, cash, cash.text, or sugar_count, because those are the objects where you try to access a property or method with dot syntax (.).
I suppose that:
- "buy" object is not created
- "cash" object is not created
- "cash.text" is not created
You can try this:
if(buy)
buy.play();
else
trace("buy object is null");
if(cash && cash.text)
cash1 = cash.text.text;
else
trace("cash object is null");
Could you be more detailed?
精彩评论