AS3 script not being called on gotoAndStop - actionscript-3

I am designing an app in Animate CC, and have a setup like this:
Each page is a separate 20 frame section of the timeline. The NavigationScript layer contains a script that handles the main navigation, and the ImageControlScript layer contains code to set up event listeners that are specific to each page:
import flash.events.MouseEvent;
image1.addEventListener(MouseEvent.CLICK, openImage(144));
image2.addEventListener(MouseEvent.CLICK, openImage(145));
trace ("Events added");`
When the app starts, everything goes fine: the event listeners are added, and the trace() is called. However, when I navigate to another page, the script on the ImageControlScript layer are not called, not even the trace function.
If I navigate back to the first page (Frame 1), it's also not called.
The navigation script uses a gotoAndStop function to move between pages.
Any help would be much appreciated, I'm not all that familiar with AS3...

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.

cocos2d-x pause game on background with pause screen

I am trying to figure out how to implement a pause screen on background. In my game scene I have a method called pauseGame. I set this up so that the user can click on pause button while playing and menu comes up asking them if they want to resume or go to main menu. Now when the phone goes into the background I want to also call this same function in my game scene so that it will show that menu so that when the user comes back to the app they will be able to resume instead of it automatically starting. I have googled for hours now and tried a few things but the only thing I am able to come up with is to call the directors pause() method from the app delegate. Which is what is happening any way but I need to call the pauseGame method of my game scene. Is there an event that I can listen for in the game scene or is there a way to call my pause from the appdelegate. I just cant seem to find an answer for this one.
App Delegate has this function, void AppDelegate::applicationDidEnterBackground(), which gets called every time app enters background.
You can access current scene by using this,
Director::getInstance()->getRunningScene(); cocos2d-x 3.0
CCDirector::sharedInstance()->getRunningScene(); cocos2d-x 2.x //Not entirely sure of the syntax, pls to check
After right typecasting and importing the appropriate header file, you can call required function from here.
Also,
AppDelegate also has, void AppDelegate::applicationWillEnterForeground(), which gets called every time app enters foreground.

how to set actionscript code to execute only once

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.

How can I make a button inside a Movieclip send me to a frame in the main timeline?

I made a movie clip with an animation of a pull-down menu in which 3 buttons appear. The thing is, I'd like for those buttons to send me to an exact frame within the main timeline, and I haven't quite understood how it can be made.
The movieclip is located within a frame inside the main timeline, and its structure is kind of like this one.
The frame "Sucesos" is not within the timeline of the MovieClip, but in the Main Timeline, whereas the Button "IrSucesos1" is located inside a frame within the movie clip. The code I use is this one.
function LinkSucesos1 (event:MouseEvent):void
{ gotoAndStop("Sucesos1");
}
IrSucesos1.addEventListener(MouseEvent.CLICK, LinkSucesos1);
I don't know whether I should put the code inside the timeline of the movieclip or on the main timeline in the frame the movie clip is located, or if the code is the right one.
How do I make the buttons work by sending me to the frame that I want to get to in the main timeline and then have it stop there? What is the code, and where should I put it?
If I've understood your structure the way I think I have, the following should work. On your main timeline, add a new layer called Actions and on the frame that has the movieclip containing your buttons add a keyframe to this layer. Go to the movieclip that contains the buttons. Open the actions panel and type Stop(). Create a new layer called Button actions, click on the (blank) kayframe on this layer and add the following code:
import flash.events.MouseEvent;
my_button.addEventListener(MouseEvent.CLICK,on_click)
function on_click(e:MouseEvent)
{
MovieClip(root).gotoAndStop(5)
}
You'll need to change my_button to the button instance you'd like to assign the event listener to and 5 to the frame you want to go to on the main timeline. Test your movie - hopefully this should work for one button. For the others to work, simply give them each their own event listener and function to do what you want them to (ask if you get lost!).
stage is available anywhere where the displayObject is linked to the stage.
You can give a reference object to that control button. Your timeline object won't change so its fine.

actionscript 3 popup window - not js

Im doing a program in AS3 and, i'm trying to do the following:
Lets say I have a button, which have onClick event, and when the event is triggered, the popup window will appear.
And in this popup window there is going to be an animation (countdown) of 2020 (this is just an example).
Is there a way to do this without javascript. Because i'm not doing this for website, its going to be a animation.
Hope i was clear.
Thanks
In Flash,create a movie clip, give it an instance name of "countdown_mc", put the countdown animation inside it.This will be your "alert" type window.
Now, in the movieclip containing "countdown_mc", add a new layer and on the same frame bring up the action editor(F9) and write the following: countdown_mc.visible=false;
When you push the button it should make countdown_mc.visible=true and maybe do an optional growing animation;
Also on this frame add a listener for countdown_mc so that when the user clicks on the window it disappears(countdown_mc.visible=false);
For animations add download tweenmax from http://www.greensock.com/tweenmax/, extract the archive in the folder that contains your fla and add the following to your code:
import com.greensock.*;
TweenMax.from(countdown_mc,0.5,{scaleX:0,scaleY:0});