Why does one of my AS3 event handlers only work once? - actionscript-3

I am building an SWF panel with support for English and Japanese, and a button to toggle between them. The English is on frame 1, the Japanese on frame 2. On the first frame there is this relevant AS3:
btnLangToggle.addEventListener(MouseEvent.CLICK, onLangToggle);
...
this.stop();
...
function onLangToggle(e:MouseEvent):void {
if (MovieClip(root).currentFrame == 1) {
MovieClip(root).gotoAndStop(2);
} else {
MovieClip(root).gotoAndStop(1);
}
trace(MovieClip(root).currentFrameLabel);
}
I click the button and the event handler function runs fine, once. If I click it again, nothing happens. Why?
Edit: Here is what my two frames and timeline look like.

If I click it again, nothing happens. Why?
Because It's a MovieClip and they are designed for animation, not for application states. Objects on the scene in first frame isn't accessible in the second key frame.
I assume in your case you have button for toggling language with different instances of MovieClip(Different languages). In second frame apply again event listener for the Japanese version of the language button:
btnLangToggle.addEventListener(MouseEvent.CLICK, onLangToggle);
Also, please read about Document Class, It's pretty simple create application with only 2 states, like you have (with 2 frames...)

I assume you are using key frames for the button, right?
You must be sure that the button is the same on both frames. Which means you must not use key frames - instead use separate layer for the button with a single keyframe and two normal frames. This way your code will work as it's the same button.

Related

Flash CS6 Button Not Working Despite No Compiler/Output Errors

I'm using Flash CS6, AS3 to create buttons for my project. Below is my code:
Intro_btn.addEventListener(MouseEvent.MOUSE_DOWN, Intro_func);
function Intro_func(event:MouseEvent):void {
gotoAndStop("Intro");
}
No errors appear when I run it on the output and compiler panel, and the same happens when I run it through the debugger. Also, I have used the exact same code for five other buttons, and they have no problems working. Please can someone tell me what is wrong with my code???
I would need to know more info of your code and the type of buttons you're using, however I would try this steps:
1.- add a Button component from the component inspector, set its instance name, and add that same function to the Click event:
newButton.addEventListener(MouseEvent.CLICK, Intro_func);
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- run your app and test if the action is executed with this new button. If this test pass, then your problem is your button.
check this on your button:
check the base class of the button (be sure its a MovieClip or a Button class)
check if your button is enabled/disabled.
check if your button or its children has mouseEnabled and mouseChildren set as
true.
check there is no other object on top of the button either in your artboard or added by code at runtime.
I finally would suggest you to set useHandCursor = true; this will replace your mouse pointer arrow to a hand when you move over your button (this is just to test that your button is actually interacting with mouse).
hope this helps.
sorry for the delay, But I've found your solution, It's easier than you thought: your frame is delayed.
your Intro_btn script is on frame 10, however in frame one you're telling it to go to frame 9 (the "Erhu" frame label) so the actions to setup the event listener are never being called.
Also keep in mind the following:
objects exists only in their frames life time, if you have an object from frame 5 to 10, it will only exist in there, so moving to a previous or later frame (e.g. frames 4 and 11) will internally delete the object in memory along with their asociated actions, in english:
you placed the button in frame 10 and added its MOUSE_DOWN listener however, if you go back to frame 0, since the button doesn't exists in this frame it will be deleted from memory along with its listeners, so if you go from frame 0 to any other frame different than 10, your button will never have its listeners associated.
So my suggestion:
1.- add your function into frame 0:
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- create a new layer. In this layer add a keyframe at the same position where your Intro_btn is(frame 10), and fill the rest of the timeline of this layer with empty frames (no keyframes), finally in the same layer add in frame 10 your listener Intro_btn.addEventListener. this way, the action is available for every subsecuent frame from frame 10.
hope this solves your problem.

ActionScript 3: eventlisteners give 1009 error when not in frame1

Thanks for the responses
*Note when the buttons and code are on Frame 1 it works perfectly *
All the script is in Frame 2, as are the two buttons. The only things not in frame to are the sprites I'm calling out of the library.
Ideally On frame 1 are navigation buttons - each button with a gotoAndPlay() call attached. When you click each one of these navigation buttons it takes you to a different page.
Each page has a a bunch of buttons. Each button, when clicked, plays a an audio, and adds/deletes children to the stage
My problem is any frame other than 1, (in this case 2) as soon as it lands on the frame even with appropriate buttons present it says it doesn't see them and balks at the even listeners.
I hope this makes sense.
----- Original post --------
I'm trying to understand how these things work. I have an empty frame in frame one. In that frame I have the code:
gotAndStop(2);
On frame 2 I have two buttons. I've added event listeners to them. This works fine. The problem is as soon as it hits frame two I get this error :
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at TesT_2_fla::MainTimeline/frame2()
The confusing thing is, when I have everything in Frame 1 it works like a charm.
On frame 2 the buttons are there already, if I put them in frame 1 it works, but in frame 2 no dice.
Can anyone explain what is happening and how I might be able to remedy this?
Not sure why you are writing the script in Frame 1. If nothing were in Frame 1, the user would automatically go to Frame 2, if you placed a stop(); on Frame 2. If you want to stop(); on Frame 1, you could add a button gotoAndStop(2) with an event listener. When placing script on a frame that may be also referenced in a later frame, use a separate script layer and extend that layer from the (key)frame on which the script first appears to the (key)frame on which it is also referenced. HTH
Ya know - after much searching I found a solution it does seem awfully cumberome. I added the following code (which I copied from someone elses post) Just in case any other noobs have a similiar problem
enter code here
//listen for the Flash player's ENTER_FRAME event...
this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false);
//and call this checker function continually until all of the buttons are accounted for
function onEnterFrame(e:Event):void
{
if(and_btn != null && big_btn != null )
{
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
//clean up the enter frame listener so that this function no longer gets called
this.removeEventListener(Event.ENTER_FRAME, onEnterFrame, false);
}
}
It seems the code was running so fat the buttons didn't have a chance to populate the stage

Actionscript 3: How to have a keyboard Keystroke "Button" go to specific Frame FROM a specific Frame?

I'm Super New to Programming in general and very New to Actionscript 3. It's been a rough but fun ride.
Anyway For the work I am doing I have my animation Play up to a certain point and it asks the viewer a question, The viewer has 3 choices, and each of those choices are connected to a button on the keyboard. A B C or D.
After searching for someone with a similar problem, My solution so far is this:
stage.addEventListener(KeyboardEvent.KEY_DOWN, cNote);
function cNote(event:KeyboardEvent):void {
trace(event.keyCode);
if (event.keyCode==67) {
gotoAndPlay(21);
}
I have this repeated 4 times each for the different Keys.
This works and allows me to move the viewer to the Frame with their response, however Any time during the animation they can press the button and it will move them to the Frame of the corresponding button (in this case Frame 21). How do I set it so that the keypress will only respond and move them to Frame 21 when they are on Frame 20 for example? (I suspect it has something to do with the "stage" part in the code but I cant get it to work without it.)
I also have the audio for the animation on a separate Layer and even though the visuals change as it goes to the correct frame, the audio continues as if the key was not pressed. Is there also a way to connect the key to make it stop the current audio and then play the new audio? Or is there a better way to do this?
Sorry if this is confusing. Thank you for your help :)
It has been quite awhile since I touch flash. From what I remembered, you can click on the frame at your movieclip, insert a keyframe by pressing f6, and press f9 to insert the listener at that frame and that frame only, after an input has been pressed, you remove it. for example:
if (event.keyCode==67) {
gotoAndPlay(21);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, cNote);
}
For the audios, I usually plays them using code, but I'm quite sure there's a way to drag and drop the audio from library to the movieclip. so I can do sound1.play(), when a button is pressed, I can do sound1.close() and sound2.play()
For more reference:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html

gotoAndPlay(label) stop all other actions on the page

I am actually at UNI doing bachelor of multimedia and we are creating a Flash movie for an assignment.
In all my fixing up, I have wrecked some code and I can't see why it won't work, because it is exactly the same as in the file that does work.
I have several buttons on one page that when clicked just go to other pages, they are working fine with functions. I have some buttons that go to a name label at a certain frame, it works for the first button, then it seems that once that button is clicked, it some all other functions on the page and no other buttons can be clicked.
Here is the code:
phonebtn.addEventListener(MouseEvent.MOUSE_UP, goPhone);
function goPhone(evt:Event):void{
gotoAndStop("phone");
}
emailbtn.addEventListener(MouseEvent.MOUSE_UP, goEmail);
function goEmail(evt:Event):void{
gotoAndStop("email");
}
addressbtn.addEventListener(MouseEvent.MOUSE_UP, goAddress);
function goAddress(evt:Event):void{
gotoAndStop("address");
}
If I put in gotoAndPlay(), it plays too long and goes to the next page.
i noticed one mistake in your function statement . that
function goEmail(evt:MouseEvent):void{
gotoAndStop("email");
}
you need to write MouseEvent whenever you are writing event for Mouse. and this is not an issue. try this. And gotoAndStop will stop on that frame. If there is any frames under that particular target frames or movieclip it will play that only not on currentTarget frames. So without seeing your frames code it is little difficult to identify your problem because you have asked on frames code. I hopes it wuld help*

Custom ActionScript 3 Play/Pause button on raw animation

I tried to search it on google but no one was matched to my search.
Anyway, I'd like to create a custom play / pause button for the animation to control the animation and the audio. I am not so familiar with ActionScript 3 so beg for me.
Here's my screenshot so you can see what i mean.
Thanks and looking forward.
You need to create a layer with one keyframe at the begin, and normal frame till the end of your animation.
In this keyframe, you'll put the button for play/pause control.
On the document class (or sigh on the keyframe itself) you'll put something like that.
myPlayPauseButton.addEventListener(MouseEvent.Click, playPauseHandler);
function playPauseHandler(e:Event):void{
if(e.target.currentFrame == 1) // button is in the play state
this.stop();
else
this.play();
}
please note that the "this" reference in the handler function, is referring to the main scene (the _root for the as2 programmers).
Note also that you can access this.currentFrame in order to know the current frame of the main animation.
myPlayPauseButton.addEventListener(MouseEvent.Click, playPauseHandler);
the Correct
myPlayPauseButton.addEventListener(MouseEvent.CLICK, playPauseHandler);