15 Replies Latest reply on Jul 30, 2009 6:20 AM by kglad

    On roll over play sound

    03bud

      I want a sound to play when a movie clip rolls over an other movie clip instance. How do I tackle it?

        • 1. Re: On roll over play sound
          kglad Adobe Community Professional & MVP

          use the sound class'es attachSound().

          • 2. Re: On roll over play sound
            03bud Level 1

            Which sound class and is it the same as attaching to a movie clip?

            • 3. Re: On roll over play sound
              kglad Adobe Community Professional & MVP

              there's only one sound class.  under as2, check the sound class:

               

              var s:Sound=new Sound(this);

               

              yourbtn.onRollOver=function(){

              s.attachSound("yoursoundID");

              s.start();

              }

              1 person found this helpful
              • 4. Re: On roll over play sound
                03bud Level 1

                OK, got this far, but am getting no sound playing  or trace in the output window.  buzz is the name of the sound and has the linkage identifier buzz_id.  Not sure if I should be using buzz _lid instead  What is the difference?

                 

                Script:

                 

                var buzz_snd:Sound = new Sound (this);

                star_mc.onRollOver = function ()
                {
                trace ("button working");
                buzz_snd.attachSound ("buzz_id");
                buzz_snd.start ();
                };

                • 5. Re: On roll over play sound
                  kglad Adobe Community Professional & MVP

                  if you're not seeing that trace(), then you're not clicking on a button with instance star_mc.  recheck your button's instance name and make sure that code executes before you click your button and after your button exists.

                  1 person found this helpful
                  • 6. Re: On roll over play sound
                    03bud Level 1

                    The sound is playing when I click on the movie clip that is placed off the stage and not playing on any of that clips six duplicate instances.  I want the sun movie clip to roll over the six instances and play.

                     

                    So how do I assign the sun clip and trigger the instances.

                     

                    Thank you.

                    • 7. Re: On roll over play sound
                      kglad Adobe Community Professional & MVP

                      declare your sound variable buzz_snd anywhere (before it's used).

                       

                      then when you want that sound to start use:

                       

                      buzz_snd.attachSound ("buzz_id");

                      buzz_snd.start ();
                      • 8. Re: On roll over play sound
                        03bud Level 1

                        I don't understand.  I thought I had by using this: var buzz_snd:Sound = new Sound (this); 

                        What do you mean?

                        • 9. Re: On roll over play sound
                          kglad Adobe Community Professional & MVP

                          // create a variable (s) that is a sound instance

                          var s:Sound=new Sound(this);

                           

                          // create a button rollover

                          yourbtn.onRollOver=function(){

                          // associate the sound instance s with a particular "sound" that has linkage id=yoursoundID

                          s.attachSound("yoursoundID");

                          // start this sound instance

                          s.start();

                          }

                          1 person found this helpful
                          • 10. Re: On roll over play sound
                            03bud Level 1

                            My apologies for not making myself clearer!!!!

                             

                            What I want to achieve is: for sun_mc (that moves using the arrow keys) to roll over six duplicates (of star_mc) one at a time and as they are deleting, that the sound buzz will play.

                             

                            That previous code does work but only on the instance of the movie clip and not on the duplicates.

                             

                            Again apologies and hope you can help me.  Thanks in advance.

                            • 11. Re: On roll over play sound
                              kglad Adobe Community Professional & MVP

                              i know i've seen your code that duplicates various objects and checks for hittests but i don't remember your code.

                               

                              so, if you can't figure out how to apply the help from this thread to your other thread that displays that code and you want more detailed help, you'll need to post the code that displays the objects you mentioned in your previous post.

                              1 person found this helpful
                              • 12. Re: On roll over play sound
                                03bud Level 1

                                I can get the sound to play on any key press, on mouse over and on playing the swf, but I can't manage to have it play on contact with the star.

                                So how do I tell the sun to make a buzz_id sound when it touches the stars?

                                 

                                 

                                var tl:MovieClip = this;
                                stop ();
                                var distance:Number = 10;
                                var keyListener:Object = new Object ();
                                keyListener.onKeyDown = function ()
                                {
                                if (Key.isDown (Key.LEFT))
                                {
                                sun_mc._x = Math.max (sun_mc._x - distance, 0);
                                //trace ("move left");
                                }
                                else if (Key.isDown (Key.RIGHT))
                                {
                                sun_mc._x = Math.min (sun_mc._x + distance, 575);
                                //trace ("move right");
                                }
                                else if (Key.isDown (Key.UP))
                                {
                                sun_mc._y = Math.max (sun_mc._y - distance, 0);
                                //trace ("move up");
                                }
                                else if (Key.isDown (Key.DOWN))
                                {
                                sun_mc._y = Math.min (sun_mc._y + distance, 570);
                                //trace ("move down");
                                }
                                //delete the sun with the cloud  
                                for (var i = 1; i <= 6; i++)
                                {
                                if (sun_mc.hitTest (tl["cloud_mc" + i]))
                                {
                                trace ("the sun hit the cloud");
                                //sun_mc's depth is less than 0, swap into
                                //an unoccupied removeable depth
                                sun_mc.swapDepths (1000000);
                                sun_mc.removeMovieClip ();
                                }
                                }
                                //delete an instance of the star with the sun  
                                for (var i = 1; i <= 6; i++)
                                {
                                if (sun_mc.hitTest (tl["star_mc" + i]))
                                {
                                trace ("the sun has hit the star_mc movieClip");
                                star_mc.swapDepths (1000000);
                                tl["star_mc" + i].removeMovieClip ();
                                }
                                }
                                };
                                Key.addListener (keyListener);
                                // This code duplicates and randomly positions new instances
                                //of the movieClip star_mc
                                for (var i = 1; i <= 6; i++)
                                {
                                duplicateMovieClip ("cloud_mc", "cloud_mc" + i, i + 6);
                                _root["cloud_mc" + i]._x = 600 * Math.random ();
                                _root["cloud_mc" + i]._y = 200 * Math.random ();
                                }
                                for (var i = 1; i <= 6; i++)
                                {
                                duplicateMovieClip ("star_mc", "star_mc" + i, i);
                                _root["star_mc" + i]._x = 600 * Math.random ();
                                _root["star_mc" + i]._y = 200 * Math.random ();
                                }
                                var buzz_snd:Sound = new Sound (this);
                                keyListener.onKeyDown = function ()
                                {
                                trace ("button working");
                                buzz_snd.attachSound ("buzz_id");
                                buzz_snd.start ();
                                };

                                • 13. Re: On roll over play sound
                                  kglad Adobe Community Professional & MVP

                                   

                                   

                                   

                                  var tl:MovieClip = this;
                                  stop ();
                                  var buzz_snd:Sound=new Sound(sun_mc);

                                  buzz_snd.attachSound ("buzz_id");

                                  var distance:Number = 10;
                                  var keyListener:Object = new Object ();
                                  keyListener.onKeyDown = function ()
                                  {
                                  if (Key.isDown (Key.LEFT))
                                  {
                                  sun_mc._x = Math.max (sun_mc._x - distance, 0);
                                  //trace ("move left");
                                  }
                                  else if (Key.isDown (Key.RIGHT))
                                  {
                                  sun_mc._x = Math.min (sun_mc._x + distance, 575);
                                  //trace ("move right");
                                  }
                                  else if (Key.isDown (Key.UP))
                                  {
                                  sun_mc._y = Math.max (sun_mc._y - distance, 0);
                                  //trace ("move up");
                                  }
                                  else if (Key.isDown (Key.DOWN))
                                  {
                                  sun_mc._y = Math.min (sun_mc._y + distance, 570);
                                  //trace ("move down");
                                  }
                                  //delete the sun with the cloud  
                                  for (var i = 1; i <= 6; i++)
                                  {
                                  if (sun_mc.hitTest (tl["cloud_mc" + i]))
                                  {
                                  trace ("the sun hit the cloud");
                                  //sun_mc's depth is less than 0, swap into
                                  //an unoccupied removeable depth
                                  sun_mc.swapDepths (1000000);
                                  sun_mc.removeMovieClip ();
                                  }
                                  }
                                  //delete an instance of the star with the sun  
                                  for (var i = 1; i <= 6; i++)
                                  {
                                  if (sun_mc.hitTest (tl["star_mc" + i]))
                                  {
                                  buzz_snd.start ();
                                  trace ("the sun has hit the star_mc movieClip");
                                  star_mc.swapDepths (1000000);
                                  tl["star_mc" + i].removeMovieClip ();
                                  }
                                  }
                                  };
                                  Key.addListener (keyListener);
                                  // This code duplicates and randomly positions new instances
                                  //of the movieClip star_mc
                                  for (var i = 1; i <= 6; i++)
                                  {
                                  duplicateMovieClip ("cloud_mc", "cloud_mc" + i, i + 6);
                                  _root["cloud_mc" + i]._x = 600 * Math.random ();
                                  _root["cloud_mc" + i]._y = 200 * Math.random ();
                                  }
                                  for (var i = 1; i <= 6; i++)
                                  {
                                  duplicateMovieClip ("star_mc", "star_mc" + i, i);
                                  _root["star_mc" + i]._x = 600 * Math.random ();
                                  _root["star_mc" + i]._y = 200 * Math.random ();
                                  }
                                  var buzz_snd:Sound = new Sound (this);
                                  keyListener.onKeyDown = function ()
                                  {
                                  trace ("button working");
                                  buzz_snd.attachSound ("buzz_id");
                                  buzz_snd.start ();
                                  };
                                  • 14. Re: On roll over play sound
                                    03bud Level 1

                                    I had to take all the last block away and then it worked.  I used all combinations of the same script and got it working many ways but the way I wanted, so learned a thing or two in the process.  I have also managed to get a sound onto the cloud as well.

                                     

                                     

                                    Many Thanks - You're a Star!!!

                                    • 15. Re: On roll over play sound
                                      kglad Adobe Community Professional & MVP

                                      you're welcome.