Actionscript 3 how to gotoAndPlay without pressing buttons - actionscript-3

So, I have movieclip named intro. When the intro runs out of frames, how do I tell it to gotoandPlay to maintimeline or upper movieclip without needing user to press any buttons? shouldnt be that hard, right? it was really super easy in actionscript 2, but in 3 I cant even seem to find help/tutorial to do this!
thanks!

Clarify what exactly you would like to play afterwards?
It could be as simple as adding a line of code to the last frame of the animation:
gotoAndPlay(3); // will goto a frame within the same move clip
Or to play a different movieclip:
MovieClip(this.parent as MovieClip).gotoAndPlay(3); //tell your parent to goto a specific frame
Or
MovieClip(this.parent as MovieClip).parentObject.gotoAndPlay(3); //tell one of your parents movieclips to start playing
EDIT:
A bit more out there method, placing this code on the parents timeline to check when the movieclip is finished:
intro.addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(e:Event):void{
if(intro.currentFrame==intro.totalFrames){
//do something
someMovieClip.gotoAndPlay(3);
}
}

Related

How do you move to another scene in actionscript 3 using buttons

I'm trying to use a button to skip from one scene to the first frame from the next scene in Flash CS6, however I keep getting the
error 2108: the scene was not found.
stop();
btnNext.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);
function fl_ClickToGoToNextScene(event:MouseEvent):void {
gotoAndPlay(1, 'scene2');
}
This is my code and ''scene2'' is spelled right, how can I correct this?
Checking the API for gotoAndPlay(), it is an overloaded argument which accepts either a String for the label, or an int for the frame. Furthermore, if a second argument is provided, it points to the frame number in the target scene. A Scene should not be confused with a frame Label. Scenes are like multiple MovieClip timelines, each with their own first frames. According to Adobe,
Using scenes is not the best approach because of a number of drawbacks...
If you are using the automatically generated names, then it should be Scene 2, and not scene2. Furthermore, although your command will work, it will appear to have done nothing unless you also add stop() to the first frame of your new scene.
Alternatively, you could just switch to the gotoAndStop() which won't cause your playhead to jump back to the first scene.
gotoAndStop(1, "Scene 2");

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.

stop object timeline from looping

I have a movie with several movieClips. One of the movieClips has a timeline with a motion tween. I created an instance of this movieClip:
var board:Board = new Board();
and used board.gotoAndStop(1); to keep the movieClip from playing at the beginning of the movie.
An eventListener checks to see when board collides with something; when it does, a function is invoked that uses board.gotoAndPlay(2) to get the board's timeline going.
I want the playhead on that timeline to stop at the end of the action, rather than looping. I tried to put a stop() on the last frame of that timeline, but Flash tells me I can't put an action on an object.
Can you help me solve this?
Thank you very, very much!
If memory serves, you need to make sure you have clicked in the frame in the timeline, so the frame is selected, and not an object on the stage. Then enter your actionscript.
Make sure you select a single frame where you would like to add actions then add your code
stop();
To make the timeline stop looping

Removing Sounds Invoked within a MovieClip from main Time Line

I currently have a difficulty removing specific sounds from playing when I advance through the timeline on a particular project.
The user chooses a particular item on the timeline which will display a specific movieclip and will then play a specific sound on MOUSE_DOWN.
The eventListener for MOUSE_DOWN which exists as follows:
stage.addEventListener(MouseEvent.MOUSE_DOWN, sprayWater);
stage.addEventListener(MouseEvent.MOUSE_UP, stopWater);
function sprayWater(event:MouseEvent):void
{
waterarm.gotoAndStop(2);
trace("SPRAYING WATER");
}
function stopWater(event:MouseEvent):void
{
waterarm.water.gotoAndPlay("waterE");
}
on frame 2 of 'waterarm' is a movieclip called 'water' that contains an animation of water and the following code to start the water sound:
var sfxWater:sfxwater;
var waterChannel:SoundChannel;
sfxWater = new sfxwater;
waterChannel = sfxWater.play();
on the frame "waterE" an animation of the water disappearing exists and the code sfxWater.stop();.
When the user progresses beyond this frame on the root timeline, the sound effect of water still remains on MOUSE_DOWN despite the movieclip no longer existing on the timeline at that point.
The ideal outcome will be the individual sound playing on MOUSE_DOWN and stopping on MOUSE_UP only when this movieclip is visible on the main timeline. If anybody can provide any assistance in being able to prevent the sound from playing and removing this event listener (through code on the main timeline if possible) it would be greatly appreciated.
Regards,
Darren
If stopping the sound is all that you want why not use SoundMixer.stopAll().
You can use it in the MOUSE_UP event after checking if the said movieclip's visible property.