-
[CS3] AS3 - Make Sound Stop/Start!
Hey everybody! The following code works exactly like I want it to by turning on the sound for my flash movie, but once the sound is on I can't figure out how to make the same button turn it off again. Can someone help me?
Code:
import flash.media.Sound;
import flash.media.SoundChannel;
status.text = "Chimes Off";
playButton.addEventListener(MouseEvent.MOUSE_DOWN, playChimes);
function playChimes(e:MouseEvent):void
{
var chimes:Chimes = new Chimes();
var chimesSound:SoundChannel = chimes.play();
status.text = "Chimes On";
var chimesStatus:Boolean = true;
trace ("Playing!");
}
-
You could add a test inside the playChimes function to see what
status.text is.
If the chimes are off, turn them on, and if they're on, turn them off.
If we use status.text for our test, we don't need the chimesStatus Boolean
(unless you're using that for something else as well).
Code:
import flash.media.Sound;
import flash.media.SoundChannel;
status.text = "Chimes Off";
playButton.addEventListener(MouseEvent.MOUSE_DOWN, playChimes);
// declare chimesSound here, so it's within scope of the else part of our
// conditional statement
var chimesSound:SoundChannel;
function playChimes(e:MouseEvent):void
{
if(status.text == "Chimes Off") // chimes are off, so turn them on
{
var chimes:Chimes = new Chimes();
chimesSound = chimes.play();
status.text = "Chimes On";
var chimesStatus:Boolean = true; // ? maybe don't need this if we're using status.text in our test
trace ("Playing!");
}
else // chimes are on, so turn them off
{
chimesSound.stop();
status.text = "Chimes Off";
}
}
-
Thankyou so much! It works!
-
©KatSOft
Use it too so THANK YOU.
Catalin.
-
Great snippet, thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|