Audio ActionScript 3 - actionscript-3

import flash.media.Sound;
var sound1 = new MenuTheme();
sound1.play(1000);
if(currentFrame == 2(your dedicated frame)
{
sound1.stop();
}
It comes up with Error TypeError: Error #1006 I want it to stop on the next frame after clicking a button otherwise this error stops everything :(
How to get audio to stop on next scene?

Rather than checking to see if the current frame is 2, I would suggest stopping the sound when the button is clicked, like so (with an example button named buttonToNextFrame):
buttonToNextFrame.addEventListener(MouseEvent.MOUSE_DOWN, goToNextFrame);
function goToNextFrame (e: MouseEvent) {
sound1.stop();
//Other stuff you would like done when the button is pressed.
gotoAndStop(2);
}
If there is a way to bypass the button to get to frame 2 and you still want the sound to stop, then you would have to either:
stop the sound in every event/way used to get to frame 2
stop the sound directly on frame 2 with a plain sound1.stop()
Let me know if you have any further questions!

Related

ActionScript 3.0 - How to make a button finish looping the scene, then go to the next scene

So, lets say scene 1 has 40 frames. Someone clicks on the button to go to the next scene at frame 10. I want it to continue to frame 40, then change to the next scene.
Keep in mind that the 40 frame scene is on loop, so it could loop 4 or 5 times, then someone clicks next scene. So, I want it to finish the loop, then go to the next scene.
I've been looking everywhere on how to do this, and can't find anything!
So, I have this code right here -
At the beginning of the movie (Frame 1-39), I have -
var clicked:Boolean = false;
button1.addEventListener(MouseEvent.CLICK, goScene2);
function goScene2(event:MouseEvent):void {
clicked = true;
}
Then, on the last frame (frame 40), I have -
if(MouseEvent.CLICK){
gotoAndPlay(1,"Scene 2");
}
else{
gotoAndPlay(1,"Scene1");
}
It goes to scene 2, even if you don't click the button.
Thanks in advance!
That's because the MouseEvent.CLICK variable is always truthy. It's the constant you use to bind a click event handler. I think you mean to use the clicked variable that you set to true in the click handler.
if(clicked){
gotoAndPlay(1,"Scene 2");
}
else{
gotoAndPlay(1,"Scene1");
}

Simple click event

I don't know why I can't figure out this problem that is really basics !! (sometimes the brain is tired I guess).
I've got a movieclip with a guitar string.
I want the string to move everytime I click on it.
I've created a movie clip with 2 lables. the first = non movement, the second = movement.
I've placed in the second one a stop(); action. (in order to stop the loop)
I've put this code :
stringOne.addEventListener(MouseEvent.CLICK, accord1, false, 0, true);
public function accord1(e:MouseEvent):void{
var stringOne;
trace("DING");
stringOne.gotoAndStop("first");
}
It works but, of course, it only play the string movement at the first click.
Do you know how I could play the string movement EVERYTIME that I click on the string ?
Thank you very much and sorry for this easy question (little ashamed)..,
EDIT
Ah ! It seems to work with goToAndPlay !
if (stringOne.currentLabel=="premier") {
stringOne.gotoAndStop("default");
} else {
stringOne.gotoAndStop("first");
}
Just a thing, I have to click twice..
(one click = the string vibrate (label2))
(one click again = the string does nothing (going to label 1))
(one click again = the string vibrate (label 2))
Is there anyway to automatically skip the 2nd click (the one that tells the string to go back at label 1), and let do the code like : - When animation of label 2 is finished, automatically go back to label 1 ? –
Presumably, the "movement" label of the string is some sort of animation?
It seems to me what you want in your "guitar string" movieClip is, on the last frame of the timeline animation for "movement", a script that says gotoAndStop('non movement'). The click handler should gotoAndPlay('movement').
Then, when the string 'movement' animation is finished, it will reset itself, so that the next time you click it will play again.
So, your original code is fine (before the edit; but remove the "var stringOne;" since that will break the reference to stringOne). The only thing you need to add is a script in the timeline on the last frame of the movement animation ("first" label?) that says gotoAndPlay("default") (assuming default is the 'non movement' label). You may need a stop() in the timeline frame for "default".
Something like that :
stage.addEventListener( MouseEvent.CLICK, onStageClick);
protected function onStageClick(event:MouseEvent):void
{
switch( event.target )
{
case stringOne:
trace("String one stuff");
break;
case stringTwo:
trace("String 2 stuff");
break;
}
}
Or add your movieClip into a Sprite and listen click event on the Sprite and not on the MovieClips.
Thank you for the answer. I've found an other way, instead of mouse click I've used mouse down for going to frame 2 and mouse up for going to frame 1

Having trouble advancing frame in AS3 to advance frames

I'm creating a quiz with buttons and I'm new to ActionScript 3.0, but very familiar with 2.0.
I have a variable inside of a MC called NextQuestion. When it reaches a certain frame, it changes to 1, then back to 0.
On the main timeline, when NextQuestion == 1, it's supposed to NextFrame();. I can't get it to work though, this is the last thing I'm having trouble with.
This is my main code with 4 buttons, 3 wrong, 1 right. The feedback MC plays an animation, and when it finishes, it sets the VAR NextQuestion to 1, which is supposed to make the main timeline advance to the NextFrame.
stop();
right.addEventListener(MouseEvent.CLICK, rightClick1);
wrong1.addEventListener(MouseEvent.CLICK, wrongClick1);
wrong2.addEventListener(MouseEvent.CLICK, wrongClick1);
wrong3.addEventListener(MouseEvent.CLICK, wrongClick1);
feedback1.addEventListener(Event.ENTER_FRAME, answerRight1);
function answerRight1()
{
if (feedback1.NextQuestion == 1)
{
trace(feedback1.NextQuestion);
nextFrame();
}
else
{
trace("do nothing");
}
}
function rightClick1(ev:MouseEvent):void
{
trace(feedback1.NextQuestion);
feedback1.gotoAndPlay("right1");
}
function wrongClick1(ev:MouseEvent):void
{
trace("wrong");
feedback1.gotoAndPlay("wrong");
}
Any help is greatly appreciated! :)
The nextFrame() method in AS3 doesn't loop trough your movieClips, it just set's the movie clip to the next frame provided there is a next frame, otherwise it does nothing.
you might want to re-write the nextFrame() to:
if (totalFrames == currentFrame)
{
gotoAndStop(1); // or gotoAndPlay(1) if you need
}
else
{
nextFrame();
}
Thats regarding why the movie clip might not start from the beginning.
Another thing that look weird is that you need a FRAME_ENTER event to check if the answer is correct. You must have a lot of spam in the output log. I'd recommend you to totally ditch the ENTER_FRAME event listener for this and do it all with the CLICKED event and dispatching the event from the feedback1 object.
As much as I understand the movie clip plays when you click a button and when the animation is over you want to either progress to next frame or do nothing. If so, on the feedback1 object add a following line in the end of animation for label "right1":
dispatchEvent(new Event("right"));
and instead of the line feedback1.addEventListener(Event.ENTER_FRAME, answerRight1); have:
feedback1.addEventListener("right", answerRight1);
That way you won't need to check the answer on every frame and you don't need to check if the answer is right or not, because when the dispatchEvent(new Event("right")); fires - it's always right.
Hope I didn't misunderstand your question :)

AS3 Button to stop Movieclip after its finished playing

Ok, so I'm a beginner at AS3 and Flash and I managed to put this code together for an animation. A Button called start_btn is supposed to start and stop a movieclip called main_mc. On the first click of the Button, the Movieclip is supposed to play (which it does), however on the second click, the movie stops in the middle of its animation (which I don't want). My question is, when you click the Button a second time, how can i get the Movieclip to finish playing its animation then stop on the last frame?
I thought about using if (main_mc.currentFrame == main_mc.totalFrames); {main_mc.stop(); but the Movieclip still does not stop on the last frame. The Movieclip itself also has a gotoAndPlay(2); command on the last frame so that the animation repeats before the Button is clicked a second time.
here is the code i have:
`start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
function mainaniS(event:MouseEvent):void
{
main_mc.play();
start_btn.removeEventListener(MouseEvent.CLICK, mainaniS);
start_btn.addEventListener(MouseEvent.CLICK, mainaniSt);
}
function mainaniSt(event:MouseEvent):void
{
if (main_mc.currentFrame == main_mc.totalFrames);
{main_mc.stop();}
start_btn.removeEventListener(MouseEvent.CLICK, mainaniSt);
start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
}`
Try main_mc.gotoAndStop(main_mc.totalFrames).
I was going to provide a quick and dirty solution, but decided instead to try and explain a few of the issues with your current implementation and attempt to refactor and explain and better one. Unfortunately I don't have access to Flash right now, so the code is untested.
You're adding and removing event listeners often, which is generally a bad idea. Instead, since you're using a single button to perform multiple functions it would make sense to track the button state in a separate variable. In this case, a boolean for whether or not the movieclip is currently playing.
var playing:Boolean;
Now we can combine the mainaniS and mainaniSt into one and perform a different action based on whether or not the movieclip is playing, and just keep the one eventlistener on the button. I've also taken the liberty of naming the method something more meaningful:
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
function onStartClick(event:MouseEvent):void
{
if(playing) {
playing = false;
}
else {
playing = true;
main_mc.play();
}
}
You may be wondering why we don't call main_mc.stop() in the first block: the reason is that you don't want to stop the movieclip as soon as you click the button, but after the movieclip has finished playing if the button has been clicked. Therefore, we just set playing to false to indicate that we want it to stop later.
Finally, we need to make sure the movieclip stops upon completion, but only if playing is false. To do this we add a listener to movieclip that is called every frame, and checks whether playing is false, and if it's on the last frame. Note that the last frame is actually totalFrames - 1: this is because the frame numbers start from zero rather than one (i.e. if totalFrames is 3, the frame numbers will be 0, 1, 2).
main_mc.addEventListener(Event.ENTER_FRAME, animate);
function animate(event:Event):void {
if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
main_mc.stop();
}
}
All the refactored code together:
var playing:Boolean;
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
main_mc.addEventListener(Event.ENTER_FRAME, animate);
function onStartClick(event:MouseEvent):void
{
if(playing) {
playing = false;
}
else {
playing = true;
main_mc.play();
}
}
function animate(event:Event):void {
if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
main_mc.stop();
}
}

Need to cancel an actionscript command stop() and continue my project

I have an Actionscript on an invisible button in my Flash project and it is supposed to stop on a frame if it is clicked. This is the script on the button:
invisobutton.addEventListener(MouseEvent.CLICK,stopframe);
function stopframe(Event:MouseEvent):void{
stop();
}
with invisobutton being the instance name of the button. It works if you click anywhere inside the invisible button the movie stops on that frame. My problem is that I do not know how to cancel the stop and continue where the movie paused. Is there a counter for the stop() command so that it can be like off and there was an on switch? I thought about using a second function for go to and play like this:
invisobutton.addEventListener(MouseEvent.MOUSE_OUT, restart);
function restart(Event:MouseEvent):void{
gotoAndPlay(*);
but I am not sure how to know what to put into the (*) so that it plays from right where it was stopped.
If someone could please advise me if there is a reverse command to stop() that can turn it off ans start again. I already tried play() but I got errors from Flash over it. I thought about start() but I had never heard of or seen such a command. Or if someone could tell me a way to correctly retrieve the frame number when the stop() command executes.
Assuming that what you want is to use the same button to toggle between play and stop, you can use the following code:
var lastFrame:int;
var isPlaying:Boolean = true;
invisobutton.addEventListener(MouseEvent.CLICK, toggleFrame);
function toggleFrame(Event:MouseEvent):void {
if ( isPlaying ) {
lastFrame = this.currentFrame;
stop();
} else {
this.gotoAndPlay( lastFrame );
}
isPlaying = ! isPlaying;
}