Cocos2d-x ios game development (9) sound effects, particle system and storage, cocos2d-xios

Source: Internet
Author: User

Cocos2d-x ios game development (9) sound effects, particle system and storage, cocos2d-xios

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:

You can first disable rotation on the screen in RootViewController. mm.

-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation {

// Set the direction

ReturnUIInterfaceOrientationIsPortrait (interfaceOrientation );

}

-(BOOL) shouldAutorotate {

ReturnNO; // not allowed

}

Delete all other codes of HelloWorld, leaving only the initialization part.

Bool HelloWorld: init ()

{

//////////////////////////////

// 1. super init first

If (! CCLayer: init ())

{

Return false;

}

Return true;

}


Add Background:

// Add a background image

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

CCSprite * bg = CCSprite: create ("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.



IOS game programming from scratch-Cocos2d-x and cocos2d engine game development who have a complete version?

Search for the mke network, 500 yuan a year

Ios game development is cocos2d or cocos2d-x, if the use of cocos2d-x, multithreading how to do, multithreading with objective-c call?

Use pthread to implement cross-platform.
Cocoa NSTread or GCD cannot be used across platforms.
You may have multi-threading of oc, but you should discard it in cross-platform development.

Related Article

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.