FrozenHaddock Tutorials: Control sounds with buttons

In this tutorial, you will learn one of the methods of controlling sound with buttons. You'll learn how to stop, and play a sound.

First create a basic button by drawing a rectangle, adding text in front of it and then grouping the two objects together (ctrl+g). Then press F8 to convert the grouped objects to a button and click ok. For more info on button creation visit here.

firstimage

Click on your button to select it and press F9 or open your ‘Actions’ panel by clicking on it. Enter the following code:

gotoandplay2

on(press){
gotoAndPlay(2)
}

This basically means when the button is pressed frame 2 will be played.
Then highlight frame 1 and open your actions panel again if it is not already open. DON’T PANIC if your script is gone, it’s supposed to be gone. Enter the following code:

stop

stop();

Now to bring your song to the stage.
Press ctrl + r, or go to File – Import – Import to Stage. Select the music you want and press ok. WARNING: not all format music files will work on flash, but most of them should.

Go to your first layer and create a new keyframe in frame 2. In your properties menu (with frame 2 still selected) go to the sound drop down menu and select your song from the list.

song

Insert a new frame into the timeline (F5) wherever you like, the further along you put it the more song you will hear. If you test your animation now (crtl + enter) you should hear the song being played.

songtimeline

Insert a new keyframe (F6) into the frame after the frame you just used.

Go back to frame 2 and break apart your button (crtl + b) (NOTE: this is only if you want to change the text on your button to ‘Stop’). Using the text tool highlight your text and change it to stop, or something along those lines. Group the button and text again (ctrl + g) and then convert it to a button again (F8).

Whilst the button is still selected open your actions panel (F9) and enter the following code:

on(press){
            stopAllSounds();
}
on(release){
            gotoAndPlay(51)
}

Note that for your animation the number at the end of gotoAndPlay will be the frame with your second play button on.

That’s sound buttons in a nutshell.

 

 

Written by: Drajon05