Copy link to clipboard
Copied
So I have this scoreboard script which is basically gives points for clicking on the correct button and subtract points if clicking on the wrong button. Make story short, the script, which run from actionscript.as file, works great as long it's on the first keyframe sequence. but when trying to run it on the next keyframe sequence it gives me this Error #1009: error message (full message bellow). Please look at my script bellow and tell me what's wrong.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MethodInfo-3()
at scoreMe2()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at scoreMe2/frame1()
My Script:
package {
import flash.display.MovieClip
import flash.events.MouseEvent;
public class scoreMe2 extends MovieClip {
public function scoreMe2() {
//Scoreboard starts here:
var score: uint;
function init(): void {
score = 100;
scorecounter.text = "SCORE:" + score.toString();
clip.buttonMode = true;
clip.addEventListener(MouseEvent.CLICK, on_press1);
gButton.buttonMode = true;
gButton.addEventListener(MouseEvent.CLICK, on_press2);
}
//Positive button adding points
function on_press1(event: MouseEvent): void {
updateScore();
}
function updateScore(): void {
gotoAndStop(2);
score += 100;
scorecounter.text = "SCORE:" + score.toString()
stop();
}
//Negative button subtracting points
function on_press2(event: MouseEvent): void {
score += -20;
scorecounter.text = "SCORE:" + score.toString();
}
init();
//Scoreboard ends here:
}
}
}
Copy link to clipboard
Copied
you're trying to reference some object that doesn't exist when your code executes.
to pinpoint the error click file>publish settings>'permit debugging'. retest.
the problematic line number will be in the error message allowing you to pinpoint the problem.
and, in general, you might want to book mark:
Debugging ActionScript 3.0 Errors, http://forums.adobe.com/docs/DOC-2542;
Debugging AcriptScript That Triggers No Errors, http://forums.adobe.com/docs/DOC-2572;
Copy link to clipboard
Copied
I already pinpointed the problem. The script is executing on the first frame when the buttons are only later on. How do I tell the script to execute at the right time? Generally it suppose to be a scoreboard that should run throughout the game. If i'm on the wrong track here please let me know. what do I have to do to run a scoreboard throughout the game?
Copy link to clipboard
Copied
you can put your references in a function and call that function when the frame that contains them plays.
Copy link to clipboard
Copied
I kinda understand what you are saying but I don't know how to go about it. Would you please give me an example?
Copy link to clipboard
Copied
// in frame 1
function addListenersF():void{
//your button/movieclip references here
}
// in the first keyframe where your button/movieclip exist:
addListenersF();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now