gotoAndStop makes it go to the next frame in Flash CS6 - actionscript-3

I have a problem: I have a movieclip ("gesprekkencivcen") with several frames and layers. I've put this action on a certain button at frame 1:
this.addEventListener(MouseEvent.MOUSE_UP, conv1);
function conv1(evt:MouseEvent):void{
MovieClip(root).gesprekkencivcen.gotoAndStop(23);
}
Now, in stead of going to frame 23 when I click the button it goes to the next frame (which is frame 2). What am I doing wrong??

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.

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

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.

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 do I stop a scene and play the next scene in adobe flash cs6 actionscript 3.0

I'm trying to make a loop animation scene where its like an eye opening and closing. I have one black rectangle that goes down and meets with another black rectangle going up to create the blink effect. Then in the middle I have a button that the viewer can click to go to the next scene. The problem is that before and after the button is pressed both scenes play simultaneously. How can I play the first loop scene and then stop it to play the next scene when a user presses that button? Thanks.
You need to tell your respective scenes to stop. Otherwise flash player will just move on to the next scene automatically after the last frame of the previous scene (and loop back to the first scene after the last frame of the final scene).
In flash, open your first scene and open the timeline view. scroll to the very end of your timeline, add a new layer, on the very last frame of your timeline on your new layer, create a keyframe (F6 on windows). With that new keyframe selected, open the code editor (F9 on windows) and put in the command stop(); OR if you want the current scene to keep looping (without moving on the next scene) put in the command gotoAndPlay(1);
If you want your other scene to stop as well at the end, repeat the above steps on it's timeline.

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);