My flash menu going crazy. AS3 - actionscript-3

I have a menu with 4 button, for switch to other content page, and all of it had this code:
function chuyenhome(e:MouseEvent):void{
homect.addEventListener(Event.ENTER_FRAME, nexttohome);
btn_home.gotoAndStop(15);
if (btn_about.currentFrame == 15){
btn_about.gotoAndPlay(16);
}
if (btn_menu.currentFrame == 15){
btn_menu.gotoAndPlay(16);
}
if (btn_contact.currentFrame == 15){
btn_contact.gotoAndPlay(16);
}
homect.gotoAndPlay(31);
}
function nexttohome(e:Event):void{
if (homect.currentFrame>=homect.totalFrames){
MovieClip(root).gotoAndStop(4);
}
}
This is menu HOME. 3 other menu ABOUT, BLOG, CONTACT button is using the same code, but renamed function. It work fine if I click only 2 menu button. But when I try to click the third menu, it's going crazy. For example, when I click at HOME and ABOUT, it switch to HOME page and ABOUT page with no problem, no matter if I click it for thousand times. It work fine, but when I try to click at CONTACT, then... it show up HOME page, I re-click CONTACT, it show up ABOUT... and so on. It's crazy. Sorry about my English. I'm using Flash CS4 and AS3. Thanks!

You have to not add multiple event listeners to one MC. See, if all the other functions are this one copypasted with altered frames, then each of them adds an enter frame listener. These are cumulative, thus calling homect.addEventListener(Event.ENTER_FRAME, nexttohome); twice will result in two calls to nexttohome per frame. Probably you can avoid this listener altogether by putting code to the timeline of homect movie clip on the last frame, to call stop(); MovieClip(root).gotoAndStop(4);

Related

as3 Button gets disabled, no error pop ups and no idea how it's happening and how to fix it

i'm working on a AS3 ball game, currently i have 2 scenes home and baseball. home has a button which sends the player to the baseball which has a little ball catching game. the button in question is on the home page, it works fine when the FLA loads and you have to click the reb button to load the game but once the game over it sends the player back to the home and the button doesnt want to load.
You'll notice that when you return to the home scene, the output continues to trace "return". This is because you are never removing your Event.ENTER_FRAME listener for exit or resetting the Score to 0.
function exit (evt:Event) : void
{
if (Score == 2)
{
Score = 0;
trace("return");
clearInterval(interval);
removeEventListener(Event.ENTER_FRAME, exit); // <--- added line
removeEventListener(Event.ENTER_FRAME,Ballgame);
gotoAndPlay(1,"home");
}
}
This will fix your issue with the button not working in your home scene, however, you'll want to add some code to clean up the display list (as it will still show all of the baseballs that you generated on the ballgame scene).

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

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.

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*

ActionScript 3.0, Flash freezes in gotoAndStop()

I am making a flash file, I am using Adobe Flash CS6 to make it and so using ActionScript 3.0 for coding, I have a kind of "a link" called "About", it should move the flash to a frame that has the "About" content which number is 290 here in my case.
Here is my code for clicking on the "About" button:
import flash.events.MouseEvent;
var frameNumber:int;
var counter:int;
function onClick3(event:MouseEvent):void
{
if (counter%2 == 0)
{
frameNumber = currentFrame;
gotoAndStop(290);
}
else
{
gotoAndStop(frameNumber);
}
counter++;
}
aboutButton.addEventListener(MouseEvent.CLICK, onClick3);
This should display the "About" frame on even clicks (starting from zero) and go back to the frame that "About" was clicked from on odd clicks, like this:
Click on "About" --> Show About.
Another Click on "About" --> Hide About and go back to the calling frame.
Another click on "About" --> Show About.
And so on..
It works well for moving between a frame and the "About" frame (290), but the problem is that I have a "Next" and "Previous" buttons too, but when I come back from the "About" page, the whole flash freezes and nothing work except the "About" button, so what is the problem here ? What can solve this ?
And if there exist, is there a way to display my "About" section as a pop-up message in flash ?

AS3 Button stops working after random amount of click

I have a movieclip being used as a button. After a random amount of clicks the button stops working. In other words, the mouse will become a hand when hovering over the button but no clicks are registering to fire the function. I've even clicked it 40 times and it will work but then suddenly, bang!, it stops working. Heres the function that adds the btn, listener, animates it into the screen and also adds text.
function makeButton():void{
addChild(myBtn);
myBtn.mouseChildren=false;
myBtn.buttonMode=true;
myBtn.x=(stage.stageWidth/2)-(myBtn.width/2);
myBtn.y=-300;
myBtn.addEventListener(MouseEvent.MOUSE_DOWN, btnClicked, false, 0, true);
myBtn.btn_text.text="The string goes here";
TweenLite.to(myBtn, 0.5,{x:(stage.stageWidth/2)-(myBtn.width/2),y:(stage.stageHeight/2)-(myBtn.height/2)});
}
And then here's the function that animates the button outside the screen:
function btnClicked(e:MouseEvent):void{
myBtn.removeEventListener(MouseEvent.MOUSE_DOWN, btnClicked);
TweenLite.to(myBtn, 0.5,{x:(stage.stageWidth/2)-(myBtn.width/2),y:-300});
}
Strange thing is, I added a trace("listener added") into the 'makeButton()' AT THE VERY END, AFTER THE ADD EVENT. And it traces everytime, even on the times the button stops working. SO i can only assume there are no errors with listener being added. But then why is it not working?
I'm stumped. I thought it could be an event propagation problem. In other words the listener was being added to the target (myBtn) but somehow it was capturing or bubbling wrong but..... then why does it work at all? And for so many clicks?
The truth is out there. Or maybe in here, your insights will be much appreciated.
Where does myBtn get created? I can see right at the beginning of makeButton() that you are adding it the display list but can't see where it actually gets created? Is it already on the stage?
Adding a trace statement in the makeButton function will only tell you that a button is created, it won't say much about the functionality of your button. If you want to check if your button reacts to a click , you need to add your trace statement in the click listener.
According to your description , it sounds like you keep adding the same button to the stage rather than actually clicking the same button.
How often do you call the makeButton function before it stops working? This function looks like it should only be called once. As for the btnClicked function , why do you remove the listener, if you wish to click the button again?
Practically it looks like you should only have your Tweening functionality in your functions, I mean , once the button is created , you need one function to tween the button, then instead of adding the button again, simply call a function to tween the button back in place.
All the rest shouldn't be repeated.
I've fixed the code and the problem hasn't occurred again. The problem must have been that I was running the addChild every time the function called and that was doing something odd to the MC in the display list. I haven't pursued the error by clicking the buttons many times in a row for a minute or two, as I did to make the error happen originally. I think i'll let sleeping dogs lie.
With that said, my code is a lot cleaner with the addChild and other crap running in the initialization function and just sitting above the stage - and then being tweened into position in the 'makeButton' and 'btnClicked' functions (which are now fittingly named 'tweenBtnIn' and 'tweenBtnOut').
Thanks again