Cocos2d-x ios game development (8) sound effects, particle system and storage

Source: Internet
Author: User

Cocos2d-x ios game development (8) sound effects, particle system and storage

We know that a game is indispensable for sound, and some nice sounds will bring you up your interest in the game. Of course, sound is not what we want to learn, our goal is to put the sound out when appropriate. By the way, we will talk about simple particle systems and file storage in this section.

I. sound playback:

1. Create a cocos2d project and add corresponding audio image resources:


Content Used for playing a sound:


Code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1el77/large + large/large + fTwvcD4KPHA + large + C0 + sLryKu2vMm + large/Cs/XKvLuvsr + 311_vcd4kpha + large authorization + ICAgIHs8L3A + signature + CgrM7bzTsbO + sko6cjxwp?vcd4kpha + signature + Z2V0V2luU2l6ZSgpOzwvcD4KPHA + signature "BG.png ");

Bg-> setPosition (CCPoint (winsize. width/2, winsize. height/2); // call the constructor after 3.0

This-> addChild (bg );

Run:


Add button:

Optional * item = CCMenuItemSprite: create (CCSprite: create ("menu0.png"), CCSprite: create ("menu1.png"), this, menu_selector (HelloWorld: onMenuItem )); // normal and click Images

Item-& gt; setPosition (CCPoint (winsize. width/2,100 ));

CCMenu * menu = CCMenu: create (item, NULL );

Menu-> setPosition (CCPointZero );

This-> addChild (menu );

Run:


Add the following sound and sound effects:

// Play music

SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic ("bg.mp3", true); // true indicates playing continuously

// Pre-loaded sound effects

SimpleAudioEngine: sharedEngine ()-> preloadEffect ("selectworkflow ");

Event click function:

VoidHelloWorld: onMenuItem (CCObject * obj)

{

// Play Sound Effects

SimpleAudioEngine: sharedEngine ()-> playEffect ("selectworkflow ");

}

Run:

A sound is triggered, and a sound is triggered when you click the button .... This allows you to play a sound.

II. Introduction of a simple Particle System


Cocos2d already has its own particle system. Of course, you can also use its own. The following code implements an internal particle system:

VoidHelloWorld: onMenuItem (CCObject * obj)

{

// Play Sound Effects

SimpleAudioEngine: sharedEngine ()-> playEffect ("selectworkflow ");

// Create a Particle System

CCSize winsize = CCDirector: sharedDirector ()-> getWinSize ();

CCParticleExplosion * ex = CCParticleExplosion: create ();

Ex-> setPosition (CCPoint (winsize. width/2, winsize. height/2 ));

This-> addChild (ex );

}

Click "run:


Because the animation is too fast and the effect is not good, you can demonstrate it yourself ..

Here I use my own particle system network and there are a lot of software for particle system. Here I use the particle designer to open it:


Open


Click Save.

Third


√ Be sure to select the type of the saved file as the plist file and add the saved plist file to the project ..

The Code implements its own particle system:

// Self-configured Particle System

CCParticleSystemQuad * Quad = CCParticleSystemQuad: create ("test. plist ");

Quad-> setPosition (CCPoint (winsize. width/2, winsize. height/2 ));

This-> addChild (Quad );

Run:


We can see that a flame is jumping, and the particle system is loaded in ..

Iii. File Storage

Cocos2d storage is similar to ios storage...

Code:

VoidHelloWorld: onMenuItem (CCObject * obj)

{

// Play Sound Effects

SimpleAudioEngine: sharedEngine ()-> playEffect ("selectworkflow ");

// Create a Particle System

CCSize winsize = CCDirector: sharedDirector ()-> getWinSize ();

CCParticleExplosion * ex = CCParticleExplosion: create ();

Ex-> setPosition (CCPoint (winsize. width/2, winsize. height/2 ));

This-> addChild (ex );

// The storage and ios local storage are almost familiar With ios development

CCUserDefault: sharedUserDefault ()-> setIntegerForKey ("px", 12); // stored by key

CCUserDefault: sharedUserDefault ()-> flush ();

Int px = CCUserDefault: sharedUserDefault ()-> getIntegerForKey ("px ");

CCLOG ("px = % d", px );

}

Click to run:


The px value is printed.


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.