How can I play a sound effect when a Usercontrol added to gamecanvas is picked up?

Oct 11, 2012 at 6:53 AM

I am play to detect a collision and play a sound effect, I have several types of user controls added to the gamecanvas and am trying to play a different sound effect for each of these control when they are picked up/tapped. is this possible?

Coordinator
Oct 11, 2012 at 5:41 PM

Take a look at the "Demo.Advanced" code (either in the Demos.Metro, Demos.SL, or Demos.WP7 subfolder) int the download.

It defines two sounds:

         _soundAddItem = new SoundMain(cnvGame, "Assets/sounds/trampoline.wav", 3, 0);
         _soundHit = new SoundMain(cnvGame, "Assets/sounds/hit.wav", 5, 0);
 

... and then plays those sounds in the Collision event and when items are added...

        void cnvGame_Collision(PhysicsSprite sprite1, PhysicsSprite sprite2, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            // this event is fired for each sprite to sprite collision
            // taking into account CollisionGroup.
            _collisionCount++;


            if (PhysicsUtilities.GetMaxCollisionForce(contact) > 0.3)
                _soundHit.Play();
        }

You can use this technique, but you would wire up the "Tapped" (or "Tap" for WP7) event for the PhysicsSprite to play the sound.