gotoAnPlay() returns to first frame after being called twice with same number - actionscript-3

I have two keyframes in my scene. In the first one, I have the following code:
import flash.events.KeyboardEvent;
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, foo);
function foo(event:KeyboardEvent):void {
gotoAndPlay(2);
}
In the second one, I have the following code:
stop();
On the first time I press a key, the scene goes to frame 2 as expected. But on the second time, it goes to frame 1.
Also, if I use gotoAndStop instead of gotoAndPlay, the scene always goes to frame 2.
Anyone knows the reason behind this behavior?

As it's defined by Adobe, gotoAndPlay()
Starts playing the SWF file at the specified frame ...
So if you are in the second frame of your scene, when you do gotoAndPlay(2) it has the same action as a simple play(), that's why you will be back to your first frame. And it's the same behavior with gotoAndStop(2) when it's executed in the second frame, it's equal to a simple stop().
Conclusion :
gotoAndPlay() when it's called from a frame and to go and play the same frame, it's equal to a simple play().
gotoAndStop() when it's called from a frame and to go and stop the same frame, it's equal to a simple stop().
Hope that can help.

Related

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

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.

If statement in AS3 not working

This is an if stament on a frame on the root. I want to loop Carrera(a lenghthy movieclip) from frame 2 back to frame 1. (For testing purposes)
This is the code:
if (MovieClip(root).Carrera.currentFrame==2){
MovieClip(root).Carrera.gotoAndPlay(1);
}
The MovieClip keeps going, ignoring the if statement.
What I'm doing working?
There's no bug with the if statement, it's just not being evaluated when you expect it to be.
When you place code in a frame, it gets executed right away, when that frame gets entered. So, when the first frame starts, the if is executed, whose condition is false at that time. And it never gets re-executed, because you never tell it to be. There is no such thing as "standing orders" in AS3 ;-)
Instead, you can check on every frame by adding an event listener:
addEventListener(Event.ENTER_FRAME, function (e) {
if (MovieClip(root).Carrera.currentFrame==2){
MovieClip(root).Carrera.gotoAndPlay(1);
}
});
Or, you can just place gotoAndPlay(1); on the second frame of Carrera (not the root).
You have to understand that you are running this if statement only once. Even if the Carrera clip is at frame 2 in that exact moment, the clip will jump to play 1 and continue playing - there is nothing to make it do the jump again, and thus there can never be a loop.
In order for this to work, you have to run this same statement again and again - every time the clip jumps to a new frame.
For example, you can do this by a) attaching this script to frame 2 of the Carrera clip (not the root!):
gotoAndPlay(1);
or b) adding an event listener to it:
MovieClip(root).Carrera.addEventListener (Event.ENTER_FRAME,
function ( ev:Event ) : void {
var cl:MovieClip = ev.target as MovieClip;
if (cl.currentFrame == 2) cl.gotoAndPlay(1);
}
There are many more ways to do this, but unless you are going to do more complicated things than jumping to frames every now and then, I would advise you to go for the first option - it seems you should learn more about ActionScript before trying out event listeners.
Things to test...
Is MoveClip(root) defined at the point in execution?
Is MoveClip(root).Carrera defined at the point in execution?
Is MovieClip(root).Carrera playing (or have you called stop on it so it's just sittin in frame 1?

stopping on the last frame (flash)

I want my movieclip to play once and stop on the last frame. I use the following code in the loop of my movieclip class. (this is as3)
if(currentFrame == 120)
stop();
120 is the last frame. It plays once. but the problem is it goes back to frame 1 again. is there a better method of stopping a movieclip on a particular frame.
If you want to do it with actionscript and not add a stop() to the last frame on the timeline manually, then you can use the undocumented addFrameScript() method.
mc.addFrameScript(mc.totalFrames - 1, function():void
{
mc.stop();
});
Can't remember the scope of the function, but you can either use mc.stop(), or just stop().
*EDIT - Added -1 to the first parameter of addFrameScript, because it is zero based (so to put a frameScript on frame 10 you would put 9).
On the timeline, you can just put a keyframe at the end, and add the stop(); method there. When it reaches that frame, the stop(); method will execute, and the clip will stop.
Assuming you are using the timeline that is, but sounds like you are.
Easiest way:
Just select the last Frame in the timeline. Open the Actions pane(F8). Ensure the frame u selected is still selected, if not then select again while the Actions pane is open.
After selecting, just add the simple stop() function in the the 'Action-Frame' pane.
//add the following line to the actions pane of the last frame.
stop();
You're done. You can optionally add a replay button if needed.
This seems more along the lines of what you're looking for:
addEventListener(Event.ENTER_FRAME, function(e:Event){
if(currentFrame == totalFrames){
stop();
removeEventListener(event.type, arguments.callee);
}
});
I added an event listener (for Actionscript 3) so on every frame this function will fire and check what frame it is on. If it's the last frame it will stop. Based on your example I'm presuming you have a class that's extending MovieClip.

In actionscript 3, how do i address objects that aren't on frame 1?

I have an object (button) on frame 2 of my timeline, with the name btnMenu. When clicked, i want to return to frame 1 in the timeline. Actionscript 3 does not allow me to bind the eventlistener to the button from my code on frame 1.
Layout:
layer 1: Actions, only on frame 1
layer 2: btnMenu, only on frame 2 (with an empty frame in front)
Code:
stop(); // don't automatically go to frame 2
btnMenu.addEventListener(MouseEvent.CLICK, function() { gotoAndStop(1); }
(and other code to go to frame 2 obviously)
The error i get is "can't find method/property of object that is null" (roughly translated).
Please help?
You can only access objects that are on the same frame where your code lives. You have several ways to get around this. Depends how you want to structure you application.
You can move all your buttons to the frame where you have the code, toggle their visibility to false and once you reach the frame where the button should be visible just set it to true.
Other way is to move the addEventListener code to the same frame of your button. You can still access your code in the first frame, and call function if you need so.
Consider the following:
// code in first frame
stop();
function goto(evt:MouseEvent):void {
gotoAndStop(1);
}
// code in button frame
stop();
btnMenu.addEventListener(MouseEvent.CLICK, goto);