FORUMS: list search recent posts

Play and Stop sounds on entering and leaving frame - AS3.0

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Ron Steele
Play and Stop sounds on entering and leaving frame - AS3.0
on Feb 4, 2010 at 4:42:50 pm

Hi there,

I am using Flash CS 3 with Actionscript 3.0

I would like to have a sound file play when the playhead reaches a frame and then stop when the playhead moves to another frame.

This is the code I currently have in the frame to play my sound (sound_1) - works fine, no problems...

var mySound:Sound = new sound_1();
mySound.play();

What do I need to add, to have the sound stop, when the playhead EXITS that frame. I understand that "soundMixer.stop" stops all sounds, but how do I activate that when the playhead leaves the frame?

Any help is much appreciated.

Thanks


Return to posts index

Brodd Nesset
Re: Play and Stop sounds on entering and leaving frame - AS3.0
on Feb 5, 2010 at 2:52:29 pm

I can think of two approaches to this right now.
1. Why does the playhead move on? It must be for a reason; either a mouseclick, a timeout, a movieclip that has finished... Couple the sound off command to this (i.e. put it in the mouseclick handler for instance)
2. You can use the currentFrame property to check which frame the playhead is in. You can store the looping frame number in a variable, and when the currenFrame is > than this, you stop the sound.



Not everything that can be counted counts, and not everything that counts can be counted.



Return to posts index

Ron Steele
Re: Play and Stop sounds on entering and leaving frame - AS3.0
on Feb 5, 2010 at 3:22:24 pm

Thank you Brod. The playhead would leave the frame because of a mouse event, but because I am using a dynamic drop down menu, it could be any one of a number of options. I guess the only way to do with would be to put the StopSoundMixer option in the handler of every possible selection so that any sound that is playing will be stopped. That will work.. it just didn't seem a very elegant solution.

I wonder why Adobe did not think of using the "onExitFrame" handler? I got used to that in Lingo.

Thanks for the help...


Return to posts index


Brodd Nesset
Re: Play and Stop sounds on entering and leaving frame - AS3.0
on Feb 6, 2010 at 9:11:46 am

Please - don't but this command in every handler! For instance you can put a listener on to the parent object.



Not everything that can be counted counts, and not everything that counts can be counted.



Return to posts index

Ron Steele
Re: Play and Stop sounds on entering and leaving frame - AS3.0
on Feb 6, 2010 at 4:40:20 pm

Thank you Brod. Forgive me, I am a complete newbie with ActionScript 3.

What would the eventListener listen for? The next action could be a mouseclick on any one of the 20 drop down menu options. Is there a function which I could use to say "listen for ANY MouseClick event on any button object" rather than on a specific button object?

I liked your second suggestion - I just haven't a clue how to write it.

Thanks


Return to posts index

Brodd Nesset
Re: Play and Stop sounds on entering and leaving frame - AS3.0
on Feb 8, 2010 at 1:05:52 pm

Sorry, I was interrupted while writing my previous post.

"Simple button navigation made difficult" is perhaps the main criticism against AS3 versus AS2. That might be right. But as soon as you start doing anything even just a little bit more complex, AS3 starts to shine. Here is for instance a very small script which contains a complete menu navigation:

myMenu.addEventListener(MouseEvent.CLICK, goTo);

function goTo(e:MouseEvent):void {
var myLabel:String = e.target.name; //Name of the selection
gotoAndPlay(myLabel);
//Commands applicable to all selections goes here
}

The idea here is to give the menu item (typically a MovieClip - or a button) the same instance name as the label it's pointing to. If a click on menu item #1 is supposed to go to label "A", then call menu item #1 "A". If you prefer longer label names go ahead - as long as you use the same name for the corresponding menu item. Tip: Don't use numbers for Label naming.

Wrap the menu items into a MovieClip, and call it for instance "myMenu". The EventListener will 'listen' to any clicks on this main MovieClip, AND you can drill down to which item was clicked by using 'event'.target.name, both in the same script.

The event model is so powerful once you get under the hood - and in AS3 it is consistent in contrast to AS2. I do recommend some reading to get you going; for instance "ActionScript 3.0 Quick Reference Guide" by Stiller. Then there are tons of tutorials and articles around the net of course, we Flash users are spoilt.

Once the more 'true OOP' nature of AS3 starts to sink in, you will actually start to recognize many concepts from Lingo! Those guys were good; ten years ago.



Not everything that can be counted counts, and not everything that counts can be counted.



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]