Stick Page Forums Archive

Button to turn off sound or switch it on [Flash][Actionscript]

Started by: cashleycool | Replies: 5 | Views: 2,774

cashleycool

Posts: 40
Joined: May 2007
Rep: 10

View Profile
Aug 7, 2007 12:42 PM #49909
Ok I tell you how,

Create two buttons that are sort of similar and represent sound on and off. For example you can use play/stop or speaker with sound waves/speaker with a cross buttons.

Import your sound file into your movie: Press (Ctrl+R) to get the Import screen, now browse to locate your sound file.

Open your Library panel (Ctrl+L), locate and right click on your sound file. From the list select Linkage... the linkage properties form will pop-up. In the identifier text box enter mix1 and tick the "Export for Actionscript" and "Export in first frame" options under Linkage.

What this Does: You are giving the sound file in the library an identifier without it actually being used in the movie timeline. This enables it to be called and used by ActionScript code.

In your movie timeline, create 3 consecutive key frames. Place your Play button in the second frame and label it as play and your Stop button in the third frame and label it as stop (The frame label is specified in the properties window). They should both be in the same position for them to look like a switch
Basic Logic: We are utilizing the inbuilt Sound Class in Flash MX to dynamically load the sound file and the on/off buttons to play and stop it.

Click the first key frame and copy-paste the following actionscript into the actions panel.

my_sound = new Sound();
my_sound.attachSound("mix1");
play();


What this Does: You are creating an instance of the Sound Class whose source is mix1 (the identifier in the library for your sound file).

Click the 2nd and 3rd frames and give the stop() action.

Select the play button in the 2nd frame and copy-paste the following actionscript code into the actions panel.

on (release) {
_root.my_sound.start(0,1000);
_root.gotoAndStop("stop");
}


What this Does: On clicking your play button, you are telling your sound instance to start playing and the timeline to go and display the stop button in the "stop" frame. The optional parameters (0,1000) for the start function are given for looping of the sound or music file. This specifies that the sound file starts playing at 0 milliseconds and is looped 1000 times (any number value depending on the duration of the sound file and how long you want it to loop).

Select the stop button in the 3rd frame and copy-paste the following actionscript code into the actions panel.

on (release) {
_root.my_sound.stop();
_root.gotoAndStop("play");
}


What this Does: On clicking your stop button, You are telling your sound instance to stop playing and the timeline to go and display the start button in the "play" frame.

That's it your sound on/off button is ready! Its that simple.

Cool Tip: You can use these buttons even within a movie clip as the sound instance is initiated in the main timeline.

To do this just do the following:

Click the first frame and in the actions panel place the following actionscript code:
my_sound.start(0,1000);
gotoAndStop("stop");


in place of the last line: play;

Your ActionScript for the first frame will look as follows.

my_sound = new Sound();
my_sound.attachSound("mix1");
my_sound.start(0,1000);
gotoAndStop("stop");



What this Does: You are telling your sound instance to play as soon as it is initiated and the timeline to go and display the stop button in the "stop" frame.

by cashleycool:Happy:
Tawnik
2

Posts: 2,292
Joined: May 2007
Rep: 10

View Profile
Aug 7, 2007 1:41 PM #49923
Thanks man,nice tut and do you know how to change volume..?
Not button,but when it reach xx frame..
cashleycool

Posts: 40
Joined: May 2007
Rep: 10

View Profile
Aug 11, 2007 8:17 AM #50595
Quote from pantonick
Thanks man,nice tut and do you know how to change volume..?
Not button,but when it reach xx frame..


Hmmmm, I will post that kind...
Rokk

Posts: 0
Joined: Jun 2025
Aug 21, 2007 12:22 PM #53300
How exalcly to "paste the code in a actions panel" ???
froggeh.
2

Posts: 27
Joined: Nov 2005
Rep: 10

View Profile
Sep 2, 2007 8:11 PM #56303
That helped me alot, thanx. Only thing, you might want to change the thing about the movie clips with sound. It only works if you go back and delete all the _root. things you find on your on/off button.



Maybe you forgot to mention it, or maybe something is going wrong for me, but I did that, and now it works.


Thanks
cashleycool

Posts: 40
Joined: May 2007
Rep: 10

View Profile
Nov 8, 2007 7:43 AM #67182
Quote from froggeh.
That helped me alot, thanx. Only thing, you might want to change the thing about the movie clips with sound. It only works if you go back and delete all the _root. things you find on your on/off button.



Maybe you forgot to mention it, or maybe something is going wrong for me, but I did that, and now it works.


Thanks

My pleasure, anyway I've been away from spp for 2 months already XD
ANd I'm gonna find a way to make a volume control:Smile:
Website Version: 1.0.4
© 2025 Max Games. All rights reserved.