26 Replies Latest reply on May 4, 2007 1:08 PM by Newsgroup_User

    Adding sound to a button

    Proddy2k
      I have added sound to a button but when I click no sound plays. The code is fine and no errors show, its only the sound not playing.

      on mouseUp me
      sound(1).play(member(352))
      end

      Thanks in advance!
        • 1. Re: Adding sound to a button
          Level 7
          >> on mouseUp me
          >> sound(1).play(member(352))
          >> end

          try this

          on mouseUp me
          sound(1).play(member("352"))
          end

          hth


          • 2. Re: Adding sound to a button
            Level 7
            I generally use puppetSound for this sort of function.

            Give your sound member a name. Like "beep" for instance.

            on mouseUp me
            puppetSound "beep"
            end

            That will play it in channel 1. To play it in another channel, try this:

            on mouseUp me
            puppetSound 2,"beep"
            end
            • 3. Re: Adding sound to a button
              Proddy2k Level 1
              Thanks for the suggestions. I tried them both but it still don't work. Any more ideas?
              • 4. Re: Adding sound to a button
                Level 7
                Sounds to me like you have a bad audio member. Does it play if you hit
                the play button on the Property Inspector?
                • 5. Re: Adding sound to a button
                  Proddy2k Level 1
                  quote:

                  Originally posted by: Newsgroup User
                  Sounds to me like you have a bad audio member. Does it play if you hit
                  the play button on the Property Inspector?



                  Yes it does play
                  • 6. Re: Adding sound to a button
                    Level 7
                    Does any code run on that button? Try this:

                    on mouseUp me
                    alert "It works"
                    end


                    If that fails to work, then either the code is not properly placed on
                    the button, or maybe you are using a FlashComponent button. If that is
                    the case, you need to click on it, go to the Property Inspector, Flash
                    Component tab, and set eventPassMode to #always.
                    • 7. Re: Adding sound to a button
                      Proddy2k Level 1
                      quote:

                      Originally posted by: Newsgroup User
                      Does any code run on that button? Try this:

                      on mouseUp me
                      alert "It works"
                      end


                      If that fails to work, then either the code is not properly placed on
                      the button, or maybe you are using a FlashComponent button. If that is
                      the case, you need to click on it, go to the Property Inspector, Flash
                      Component tab, and set eventPassMode to #always.



                      I replaced the code with yours above and it shows no errors. When I run the application and click on the button, nothing happens, it just goes to the next page as expected but no sound or alert. The button is a Bitmap sprite.
                      Any more ideas?
                      • 8. Re: Adding sound to a button
                        Level 7
                        You need to drag the script onto the sprite for it to attach.

                        Or if you click the sprite in the score, in the Behavior Inspector, hit
                        the plus sign (+) and add the behavior to the sprite.
                        • 9. Re: Adding sound to a button
                          Proddy2k Level 1
                          quote:

                          Originally posted by: Newsgroup User
                          You need to drag the script onto the sprite for it to attach.

                          Or if you click the sprite in the score, in the Behavior Inspector, hit
                          the plus sign (+) and add the behavior to the sprite.



                          Yes the button has had the behavior attached to it but still no luck.
                          • 10. Re: Adding sound to a button
                            Level 7
                            > on mouseUp me
                            > sound(1).play(member(352))
                            > end

                            Er, just a question: is your member 352 of the castLib 1, aint'it?

                            Simone

                            • 11. Re: Adding sound to a button
                              Proddy2k Level 1
                              quote:

                              Originally posted by: Newsgroup User
                              > on mouseUp me
                              > sound(1).play(member(352))
                              > end

                              Er, just a question: is your member 352 of the castLib 1, aint'it?

                              Simone




                              Yes it is member 352 and part of the Cast. What is castLib1?
                              • 12. Re: Adding sound to a button
                                Level 7
                                If you have a bitmap sprite with this code on it

                                on mouseUp me
                                alert "It works"
                                end

                                and the alert never shows up when you click it, then there is something
                                wrong with your copy of Director, or maybe your file is corrupted, or
                                you are mistaken when you say that it is a bitmap or you are mistaken
                                when you say that the behavior is on the sprite. The fact that you get
                                no errors means that Director doesn't see anything wrong. The fact that
                                you don't see the alert means that Director does not think that it needs
                                to run that script.

                                Try making a brand new Director movie as a test. Put a single vector
                                shape on the stage (like a rectangle from the toolbar). Attach that
                                script to the rectangle. Hit Play. Click the rectangle. Does that work?
                                • 13. Re: Adding sound to a button
                                  Level 7
                                  > Yes it is member 352 and part of the Cast. What is castLib1?

                                  CastLib 1 is the first of all the castLibs, usually the "internal" one. If
                                  you had more than one I guess you'd know. For in that case you should use
                                  the syntax

                                  sound(1).play(member(352,[number|name of castLib])

                                  But I think this is not the case.

                                  What happens if you try and put sound(1).play(member(352)) in the message
                                  box and hit RETURN? Does it play correctly?
                                  If it does not, try with another very little sound, just to experiment.
                                  If instead it works correctly, try a "fresh" situation:
                                  1 - an ampty frame
                                  2 - in order to stay on the frame set the frame script to
                                  on exitFrame me
                                  go the frame
                                  end
                                  3 - put the bitmap on the stage
                                  4 - with the bitmap still selected create a new behavior
                                  (i.e. from the behavior inspector, click on
                                  "+">new behavior> name: "play my sound".
                                  5 - select the behavior yu've created in the behavior inspector
                                  and click "script window"
                                  6 - in the script window write:
                                  on mouseUp me
                                  sound(1).play(member(352,[number|name of castLib])
                                  end
                                  7 - put the movie in Play
                                  8 - click on the bitmap

                                  If after all this stuff it does NOT work, I have no other clue.
                                  If it works, then control what in this scheme differs from your situation

                                  • 14. Re: Adding sound to a button
                                    Proddy2k Level 1
                                    quote:

                                    Originally posted by: Newsgroup User
                                    If you have a bitmap sprite with this code on it

                                    on mouseUp me
                                    alert "It works"
                                    end

                                    and the alert never shows up when you click it, then there is something
                                    wrong with your copy of Director, or maybe your file is corrupted, or
                                    you are mistaken when you say that it is a bitmap or you are mistaken
                                    when you say that the behavior is on the sprite. The fact that you get
                                    no errors means that Director doesn't see anything wrong. The fact that
                                    you don't see the alert means that Director does not think that it needs
                                    to run that script.

                                    Try making a brand new Director movie as a test. Put a single vector
                                    shape on the stage (like a rectangle from the toolbar). Attach that
                                    script to the rectangle. Hit Play. Click the rectangle. Does that work?



                                    I created a new Director movie and put a rectangle on the Stage. I attached the code above and the alert came up. I then replaced the code with the sound code and it worked.
                                    • 15. Re: Adding sound to a button
                                      Level 7
                                      OK then. The code works. The problem must be that in your project, you
                                      have either not done one of those steps correctly or there is something
                                      else overriding your mouseUp event. Do you have multiple scripts on
                                      that sprite that have mouseUp? That would not work. There is something
                                      specific to your program that is having trouble. Your job now is to
                                      find it.
                                      • 16. Adding sound to a button
                                        Proddy2k Level 1
                                        quote:

                                        Originally posted by: Newsgroup User
                                        OK then. The code works. The problem must be that in your project, you
                                        have either not done one of those steps correctly or there is something
                                        else overriding your mouseUp event. Do you have multiple scripts on
                                        that sprite that have mouseUp? That would not work. There is something
                                        specific to your program that is having trouble. Your job now is to
                                        find it.



                                        Yes , the sprite has three scripts in total. Does that mean I can't attach the sound code to it? Is there a limit to the amount of behaviors you can add to a sprite or the amount of sounds you can use?
                                        • 17. Re: Adding sound to a button
                                          Proddy2k Level 1
                                          I think I may have worked out. Basically, I made sure the sound behavior was first before the other ones and now the sound is played when I click the button but the background music now stops as soon as I go to the next page? Any ideas how I can overcome this?
                                          • 18. Re: Adding sound to a button
                                            Level 7
                                            It means you should have one script with mouseUp on it. You can copy
                                            the script out of one mouseUp handler and into another. The point is
                                            that only one on mouseUp handler will run unless you go to great lengths
                                            to pass the event through your hierarchy. It is almost never worth it
                                            to have multiple instances of the same handler in different scripts on
                                            the same sprite. It always causes confusion.

                                            If you have 1 script that has code like this:

                                            on mouseUp me
                                            puppetSound "beep"
                                            end

                                            and another script with this:

                                            on mouseUp me
                                            alert "It works"
                                            end

                                            Then you can remove one of them and change the other to look like this:

                                            on mouseUp me
                                            puppetSound "beep"
                                            alert "It works"
                                            end
                                            • 19. Re: Adding sound to a button
                                              Level 7
                                              I'm guessing that you have the background sound and the button sound
                                              running in the same channel. You have 8 sound channels to play with, so
                                              make sure that you don't put them into the same one. See my previous
                                              post about puppetsound with multiple channels:


                                              on mouseUp me
                                              puppetSound 2,"beep"
                                              end

                                              That will play the sound in channel 2 (change it to any integer between
                                              1 and 8 to use different channels)
                                              • 20. Re: Adding sound to a button
                                                Proddy2k Level 1
                                                quote:

                                                Originally posted by: Newsgroup User
                                                I'm guessing that you have the background sound and the button sound
                                                running in the same channel. You have 8 sound channels to play with, so
                                                make sure that you don't put them into the same one. See my previous
                                                post about puppetsound with multiple channels:


                                                on mouseUp me
                                                puppetSound 2,"beep"
                                                end

                                                That will play the sound in channel 2 (change it to any integer between
                                                1 and 8 to use different channels)



                                                I put the background in channel 1 and now it is sorted. You talk about 1-8 channels, I only see 1 and 2, where are the rest?
                                                • 21. Re: Adding sound to a button
                                                  Level 7
                                                  Channels 1 and 2 have visual channels in the score. Channels 3-8 are
                                                  accessible only through Lingo
                                                  • 22. Re: Adding sound to a button
                                                    Proddy2k Level 1
                                                    quote:

                                                    Originally posted by: Newsgroup User
                                                    Channels 1 and 2 have visual channels in the score. Channels 3-8 are
                                                    accessible only through Lingo



                                                    Ok thanks for that information, really appreciate your help. I am also trying to add a sound to a cancel/close button but the sound don't play despite me doing what I did to the other buttons. The code on the button is:

                                                    on mouseUp
                                                    halt

                                                    Does this mean I can't have the button end with a sound?
                                                    • 23. Re: Adding sound to a button
                                                      Level 7
                                                      well, once the command 'halt' happens, the program stops, so polaying
                                                      sound won't help there. You need to play the sound, and wait until the
                                                      sound is done playing, then halt.
                                                      • 24. Re: Adding sound to a button
                                                        Proddy2k Level 1
                                                        quote:

                                                        Originally posted by: Newsgroup User
                                                        well, once the command 'halt' happens, the program stops, so polaying
                                                        sound won't help there. You need to play the sound, and wait until the
                                                        sound is done playing, then halt.



                                                        How can I achieve this?
                                                        • 25. Re: Adding sound to a button
                                                          Proddy2k Level 1
                                                          Anyone have any ideas. I have tried but no luck yet?
                                                          • 26. Re: Adding sound to a button
                                                            Level 7
                                                            Play your sound:

                                                            puppetsound 2,"end noise"

                                                            then go to your closing frame... lets call it "TheEnd"

                                                            go "TheEnd"

                                                            Then put this script on your "TheEnd" frame

                                                            on exitFrame
                                                            if (sound(2).isBusy()) then
                                                            go to the frame
                                                            else
                                                            halt
                                                            end if
                                                            end