6 Replies Latest reply on Dec 15, 2012 2:37 PM by silicon123

    Press a button...

    KillerHurdz
      Hey, i'm learning flash and was wondering... I want to make it so that when I click on a certain button, it plays a new music clip. I am trying to do it by selecting the button I want, then going into behaviors, then +, then Sound, Play Sound... It then asks for the instance of the audio file that I want to play... but I don't know what it is. I don't even know what an instance is... lol. Can someoen help me out?

      Thx,
      KillerHurdz.
        • 1. Re: Press a button...
          KillerHurdz Level 1
          Not to be a bother or anything, but I would really like it if this could be answered, thanks.
          • 2. Press a button...
            schnizZzla
            I suppose nobody answers you because you're asking how to walk but don't even know how to crawl... ;-)

            First I would not recommend to use the behaviors. They will produce some automatic code which will be hard to understand for you. You can get the same result with like 5 lines of code...
            Try the following steps (I'm working on Windows with Flash 8, so if you have another system it may be different...)

            - go to file menu and open a new FLA (Flash Document)
            - go to file->import->import to library
            - choose a wav or mp3 sound and click open so you can nsee the sound in the library now
            - right click it and choose "linkage"
            - Check "export for ActionScript" and give it a name in the "Identifier" textfield, just call it "sound1"
            - Confirm the dialog. Now create a new symbol in the library to make a button (right click in library -> new symbol)
            - In the "Create New Symbol" dialog choose button as type, give it a name and confirm with ok
            - Draw a rectangle or something so that we have something to click on.
            - Now change back to the main frame in the scene and drag an instance of your button to the scene

            Everything you need is in the scene, now the coding part:
            - click on your button in the main frame (dont double click) just that it is highlighted
            - now open up the actions and attach the following code to your button:

            on (press)
            {
            mySound1.start();
            }

            - then click somewhere in the scene to deselect the button and open the actions on the main frame
            - attach this code:

            var mySound1:Sound = new Sound();
            mySound1.attachSound("sound1");

            Voila! If you now start your animation (CTRL+ENTER) it plays a sound when the button is pressed.
            Here you can download it:
            sound.fla
            sound.swf

            If this was too much for you, I recommend you read some basic flash tutorials on the net or buy a good book.

            Have fun
            1 person found this helpful
            • 3. Re: Press a button...
              KillerHurdz Level 1
              Jeese man, thx. Easy to understand, got it done in like 5 mins. Thx for the help, works great. One last question if it's not too much... how would I make it so that when I click on a different button, it plays another audio file BUT stops the other audio file from playing. If this is too much then I can live with this, but that would be a great improvement for what im doing.

              Thx again,
              KillerHurdz.
              • 4. Press a button...
                schnizZzla Level 1
                I think I can answer this, too ;-)
                But before that I give you the advice to use the F1 button in flash :-) It's a great helper if you eplore unknown terrain. Maybe it's a littble bit hard to read when you start with flash, but has almost anything you need.
                If you just type "Sound" in flash then press F1 it takes you to the reference of the class Sound. Where you find all important methods and properties of the Sound class.
                There you are Sound.stop() is all you need. Just change the code on the button to something like this:

                on (press)
                {
                mySound2.stop();
                mySound1.start();
                }

                This is ok if you have like two or three buttons. If there are more I would prefer to use a loop to stop all other sounds.
                Or even better, if there should be no other sounds than the one sound the button triggers just use the global stopAllSounds() function like this:

                on (press)
                {
                stopAllSounds();
                mySound1.start();
                }

                It's easy, isn't it?
                Feel free to come back and ask again if you have problems.
                1 person found this helpful
                • 5. Re: Press a button...
                  schnizZzla Level 1
                  Just an important note:
                  This way we playback the sound is ok for small soundfiles. But if you plan to playback bigger mp3 songs it's not the right way, because all the sound files are included in the swf file.
                  You can control which compression is used when you righ click the sound and chose "properties". So big sound file will result in a big swf file or you really chose extreme compression and with this bad sound quality. So your visitors have wait long before the animation even start or have ugly sound.
                  That's why there is a possibility to stream files...
                  1 person found this helpful
                  • 6. Re: Press a button...
                    silicon123

                    Thank you man u r so good.