Reputation: 1
I'm trying to add multiple buttons in one Flash file (as most Flash files have).
Here is the code:
stop();
import flash.events.MouseEvent;
playButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void{
var buttonName:String = event.currentTarget.name;
if(buttonName == "playButton"){
gotoAndStop(2);
}
else if(buttonName == "settingsButton"){
gotoAndStop(3);
}
}
So what is happening is the play button works fine, but the settings button does not.
Also, later I'm going to transfer all that code to an ActionScript file, and use the include
thing on the frame that everything is on at the moment, so no need to tell me that code on the timeline is bad (which I don't understand; can someone please explain that as a bonus? No need, it'd just be nice).
If I just do this:
stop();
import flash.events.MouseEvent;
playButton.addEventListener(MouseEvent.CLICK, clickHandler);
settingsButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void{
var buttonName:String = event.currentTarget.name;
if(buttonName == "playButton"){
gotoAndStop(2);
}
else if(buttonName == "settingsButton"){
gotoAndStop(3);
}
}
I get error 1009:
TypeError: Cannot access a property or method of a null object reference.
at Untitled_fla::MainTimeline/frame1()
Upvotes: 0
Views: 115
Reputation: 1624
You did not really state your problem. But if you want to add multiple buttons then just add the event listener to multiple buttons and it's done.
playButton.addEventListener(MouseEvent.CLICK, clickHandler);
settingsButton.addEventListener(MouseEvent.CLICK, clickHandler);
Upvotes: 1