Silverlight Q & A Game 14th sound effects

Source: Internet
Author: User

In the previous chapter, we made a lot of dynamic effects. As a game, apart from visual effects, we also needed music effects. This article will add some sound effects to the game. For example, music prompts will be sent when an item is found, and background music will be generated during the game, so that the game can be more perfect and realistic.

Let's take a look at the effect after adding sound effects (because loading of music files will be slower ):

 

1First, add two music files toAudioFolder, and set build actionContentIn this wayProgramThe music file is encapsulated in the xap file:

 

2. Next, addPlaysoundactionWhen you click on a product, the sound will pop up. (magic_wand.mp3 ):

Set playsoundaction as follows, and select SourceMagic_wand.mp3File:

Similarly, you can copy playsoundaction to another 12 paths and run the program to see how it works.

 

3. Finally, let's make the background music. Wounds of the past is a song in the original sound album of ghost WARRIOR 3. It is a mysterious and miserable background music suitable for this game, what's more, the size is relative to 4 ~ 5 MB files are relatively small, and the loading speed will be faster during demo.

To put it bluntly, you must first add a mediaelement control in layoutroot and name itMusicmediaelementUsed to play background music:

Select wounds_of_the_past.pdf in the media Repository:

 

4After running the program, you will find that the background music is played only once. In fact, we want the music to be played cyclically. To achieve this function, create a behavior for loop playback and create it in interactivity.ContinuousplaymediabehaviorFolder and addContinuousplaymediabehavior. CS:

The key to this behavior is associatedobject_mediaended. After the music ends, it sets the music time to 0 and plays it again:

 Public class  Continuousplaymediabehavior : Behavior < Mediaelement > { Public Continuousplaymediabehavior (){} Protected override void Onattached (){ Base . Onattached (); associatedobject. mediaended + = associatedobject_mediaended ;} Protected override void Ondetaching (){ Base . Ondetaching (); associatedobject. mediaended-= associatedobject_mediaended ;} Void Associatedobject_mediaended ( Object Sender, Routedeventargs E) {associatedobject. Position = New  Timespan (0); associatedobject. Play ();}}

 

5. Add continuousplaymediabehavior to musicmediaelement to achieve the loop playback effect:

 

Source codeDownload:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.