[Cocos2D-x learning] 9. Music dazzle-sound effects and special effects

Source: Internet
Author: User

[Objective]: to create a simple desktop game

I. Game concepts

The idea of a game is a simple flash game. After my work is done, I found that the Android platform also has a similar game called music. The interface is similar to mine clearance. It is a grid array. The selected grid will make a sound with the beat, and some special effects will be displayed when the sound is made. Players can combine various types of music by selecting different grids.

There are two purposes for this game: one is to learn the realization of sound effects, and the other is to learn the realization of special effects.

II. Specific Research on game mechanisms

The game interface is as follows:

As mentioned above, this is a 10*10 square array. The box is gray when it is not selected, and the box is white. You can click a square to select or invert it. You can select multiple items by sliding.

After the game starts, it will scan the first column, scan the second column every ms, and so on. The white lattice on the column to be scanned will make a sound, the grids on different rows make different noises, but the sounds on the same rows are the same. When a sound is triggered, the grid will generate a special effect of explodingring in the cocos2d demo program.

3. Voice

1. Format Selection

The first question facing this game is how to prepare audio resources. Let's first look at what "sound effects" formats are supported by the cocos2d-x (here the emphasis on sound effects, because the background music seems to be different ):

Android: MP3, WAV, and Ogg

Win32: MP3, WAV, and mid

IOS: MP3, CAF (not tested)

It should be noted that the Ogg format is not supported on Win32. I tried it once. Although it won't crash, it won't sound completely. Although the Ogg format is optimal on Android, I chose to use all MP3 formats to facilitate debugging on Win32. Of course, converting to Ogg format is not troublesome.

2. Prepare audio files

Our goal is to make 10 Music notes about Ms. I am not sure what tools are used for game development.
Band in a box 2012.

After entering, I felt very professional. Fortunately, I learned piano when I was a child. Here we will only describe the parts related to this objective:

For our goal, we only need one piece of music, no cycle, and no accompaniment or anything. Therefore, click the accompaniment style and cancel "style is enabled ", set the music length to 1 and the number of cycles to 1. deselect the cycle check box and adjust the rhythm as needed. It seems that I set it to 300.

Set the cycle from edit-> set time signature. Set the cycle to 1/4.

Click the note button to edit the current section (in fact, it is also the only one). The interface is shown below.

 

Click Edit to enter the editing mode. At this time, the corresponding note appears when you click any position on the Five-line spectrum. If you click an error, you can drag the note. However, the default length is full. You can change the duration by right-clicking edit node in the menu.

After editing, you can click play to try it out. If OK, you can click make standard MIDI file under file or directly F6 to output it into a MIDI file, note that we only need one syllable here, so when confirming the output, remove include 2 bar lead-in Midi File in options.

3. audio file conversion

The obtained mid file is not easy to use on the Android platform. It is recommended to convert it to Ogg or mp3. Used here
A software such as mid to MP3 is first converted into MP3 format.

After such an MP3 format is obtained, other tools can be used for arbitrary conversion. We strongly recommend that you
Magic film factory. Easy to use, no charge, it's just overbearing. The only thing to note is that you can specify a magnification of the volume during the conversion. If the source sound is not loud enough, you can remedy it now.

 

4. Playback of audio files

In Cocos, playing sound effects is relatively simple. First, put the audio file under the Resource folder. Then, call

    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(m_SoundFile);

The header file of the dependency is simpleaudioengine. h. The parameter is the file name and can be an absolute path (this is useful if it is not placed in the default resource folder ). There is also a two-parameter version that allows you to set whether to play continuously.

If you want to optimize it to a certain extent, you can also pre-load these audio files:

void SoundBlock::initSoundPool() {    for ( int i = 0; i < 10; i ++ )        CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(SOUND_FILES[i]);}

Of course, do not forget to release these resources at the end:

void SoundBlock::freeSoundPool() {    for ( int i = 0; i < 10; i ++ )        CocosDenshion::SimpleAudioEngine::sharedEngine()->unloadEffect(SOUND_FILES[i]);}

In fact, simpleaudioengine is still relatively weak and does not support many functions. If it is on iOS, you can also use cdaudiomanager, which is much more powerful.

Iv. Particle Effects

The sound effect is ready, and now you are ready to prepare visual effects. Here we plan to use simple particle effects.

1. Basic particle Effects

Cocos has prepared several common particle effects. For details, refer to ccparticleexamples. cpp. For example, ccparticlegalaxy. The usage is as follows:

Emitter = ccparticlegalaxy: Create (); emitter-> retain (); // note the retain emitter-> settexture (cctexturecache: sharedtexturecache () -> addimage ("fire.png"); emitter-> setposition (CCP (m_size/2, m_size/2); emitter-> setduration (0.3f ); emitter-> setautoremoveonfinish (true); addchild (emitter, 10 );

The code is basically self-explanatory.

2. Additional article: Water Wave grid action

In addition to particle effects, grid actions can also achieve many special effects. For more information, see ccactiontiledgrid. cpp. A grid action is actually an action that can be executed through runaction. For example, if you want to use water waves, you can use ccwavestiles3d.

3. exploding Ring

In my current understanding, the key time for particle effects lies in the adjustment of a large number of parameters in the ccparticlesystem. Cocos can be specified one by one in the Code to facilitate programmers, you can also specify the parameter values by writing a list. The special effect XML here is the explodingring. plist under the testcpp project of direct copy. In the code, it is specified as follows:

// Playback effect emitter = new ccparticlesystemquad (); emitter-> initwithfile ("explodingring. plist "); emitter-> setposition (CCP (m_size/2, m_size/2); emitter-> setautoremoveonfinish (true); addchild (emitter, 10 );

However, the maximum number of particles set in the original explodingring is more than 800, because I have designed the particle effect for every square to be voiced, therefore, when multiple squares produce particle effects at the same time, the frame rate is only a dozen frames. You can modify this parameter value based on actual conditions.

V. Code Implementation

After completing the above two steps, the rest is very simple. See the source code.

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.