Introduction to cocos2d Game Engine

Source: Internet
Author: User
Document directory
  • I. Introduction to the Cocos2d Game Engine
  • Ii. Cocos2d game engine installation
  • 3. Basic concepts of Cocos2d
  • 4. Cocos2d game action
  • 5. Cocos2d Touch events
  • (6) Frame Animation Sequence creation tool Zwoptex
  • Add an animation
  • Playing sound

Directory:

Cocos2D game engine introduction Cocos2D game engine installation Cocos2D basic concepts Cocos2D game action Cocos2D Touch event Frame Animation Sequence production tools introduction I. Cocos2d game engine Introduction

Cocos2d is an open-source framework used to build 2D games, demonstration programs, and other graphic interface interactive applications.

Main functions: 1. flow control: it is very easy to manage process control between different scenarios. 2. sprites: quick and convenient genie 3. actions: Tell the elves what to do. More actions that can be combined, such as move, rotate, and scale. special Effects: These Effects include waves, twirl, and lens. flat Maps: supports rectangular and hexagonal flat Maps. transitions: migrate from one scenario to another scenario with different styles. menu (Menus): create an internal menu 8. text Rendering: supports tag and HTML Tag actions 9. documentation: programming guide + API reference + video tutorial + many simple test examples that teach you how to use 10. based on Pyglet: no external dependencies 11. openGL-based: hardware acceleration 2. Cocos2d game engine installation

(From 0.99.0 version, Cocos2d-iPhone template class requirements Apple SDK3.0 and later versions.

1. Download: cocos2d-iphone Latest Version 0.99.5; 2. Open the Terminal tool, enter the Cocos2d-iPhone-0.99.5 directory; 3. Execute the command:./install_template.sh

4. After the installation is complete, start Xcode and choose File> New Project from the menu. view 3. Basic concepts of Cocos2d are displayed.

To develop a cocos2d application, you must first understand the following four concepts:

1. Scenes (scenario)

Is an independent block in an app workflow. An app may have multiple scenes, but only one of them can be activated at any time. A CCScene object consists of one or more layers, which are connected to each other. Layer provides the appearance and behavior of scene.

2. Director (Director)

CCDirector is responsible for moving forward or backward between scenes. CCDirector is a shared object in singleton mode. It knows which scene is active.

3. Layers (layer)

A CCLayer object defines a descriptive area and a descriptive rule. When writing a cocos2d program, most of the work is to write the CCLayer subclass to achieve the desired effect.

4. Sprites)

A sprite object is a 2d image that can be moved, selected, scaled, or animated. A child member of a CCSprite object can contain other CCSprite objects.

Iv. Cocos2d game action (I) Basic Action 1. instantaneous action-Place completed immediately-Place hidden-Hide display-Show visible switch-ToggleVisibility2. delayed action-it takes some time to move to-CCMoveTo move-CCMoveBy jump to-CCJumpTo jump -- CCJumpBy Bessel -- CCBezierBy to -- CCScaleTo to enlarge -- CCScaleBy to -- CCRotateTo to rotate -- CCRotateBy to flicker -- CCBlink to -- CCTintTo tone change -- intby to become darker -- CCFadeTo from none from bright to none-The CCFadeOut delay action refers to the completion of the action takes some time. Therefore, actionWithDuration is the first parameter for execution of delayed actions, and the common base class of delayed actions is CCIntervalAction. (2) combined action combination refers to the combination of different basic actions according to a certain number of times to form a consistent set of combined actions. Composite actions mainly include the following types: sequence CCSequence: arrange several actions in line order, and then execute them one by one in order. Synchronous Spawn: Execute several actions concurrently, but all actions must be executed simultaneously. Repeate: Repeate a limited number of times. Reactionary Reverse: Reverse or Reverse execution of an action, supporting the reactionary action sequence. Animation: allows the genie to continuously execute an image to form a simulated motion.

RepeatForever: executes an action or action sequence indefinitely until it is stopped. (3) CallFunc ActioCallFunc Action allows you to call a method in an action. The last call in the action sequence is very useful. Example:

Id actionTo = [MoveTo actionWithDuration: 2 position: ccp (s. width-40, s. height-40)];
Id actionby = [movebyactionwithduration: 2 position: CCP (80, 80)];
Id actioncallfunc = [callfunc actionwithtarget: Self selector: @ selector (doatask)];
Id actionsequence = [sequence actions: actionto, actionby, actioncallfunc, nil];


-(Void) doatask
{

// Somecode
} There are two types of callfunc: callfuncn and callfuncnd callfuncn. node is treated as a parameter. Callfuncnd requires a node and a pointer to a certain data. Example:

Id actioncallfuncn = [callfuncn actionwithtarget: Self selector: @ selector (doataskn :)];
Id actioncallfuncnd = [callfuncnd actionwithtarget: Self selector: @ selector (doataskn: Data :) [/PRE] data: pointertosomedata];
-(Void) doataskn: (ID) node
{// Some code
}
-(Void) doatasknd: (ID) node data :( void *) d
{// Some code
}

(4) The basic movements and composite actions of speed changes the movements and animation effects of the genie, but the speed of such changes remains unchanged, through the CCEaseAction class and CCSpeed class, you can easily modify the speed at which the genie executes the action: From fast to slow or from slow to fast. EaseIn: from slow to fast. EaseOut: From fast to slow. EaseInOut: from slow to fast and then from fast to slow. EaseSineIn: from slow to fast. EaseSineOut: From fast to slow. EaseSineInOut: from slow to fast and then from fast to slow. EaseExponentialIn: from extremely slow to extremely fast. EaseExponentialOut: from extremely fast to slow. EaseExponentialInOut: from extremely slow to extremely slow.

Speed: manually set the Speed. You can also adjust it through SetSpeed. 5. Cocos2d Touch events iPhone OS transmits various combinations of Touch Information from hardware sensors through NSSet. Event Processing framework 1. (void) touchesBegan :( NSSet *) touches

WithEvent :( UIEvent *) event {}

● 2. (void) touchesMoved :( NSSet *) touches

WithEvent :( UIEvent *) event {}

● 3. (void) touchesEnded :( NSSet *) touches

WithEvent :( UIEvent *) event {}

● 4. (void) touchesCancelled :( NSSet *) touches

WithEvent :( UIEvent *) event {}

Event Processing Mechanism

Core: It is how to pass the System user input information to each Layer object.

1. Take over: Obtain the touch input from the standard UIView of the iPhone OS. 2. Distribution: distributed to various registered objects according to the predefined logic. 3. Processing: how to coordinate and respond to user input between registered objects. (6) Frame Animation Sequence creation tool Zwoptex in cocos2d, most animations are pre-rendered bitmaps. Then, the images are played together through an animation. Zwoptex Purpose 1. merge multiple individual PNG images into a large PNG image. save the position coordinates and image size of each small PNG Image in the merged PNG image through the parameter file record for Cocos2d program to call.

Add an animation

Add animations using png and plist files produced by Zwoptex

 // Loads the png Image Sequence and plist of flowers into the scenario.

CCSpriteFrameCache * cache = [CCSpriteFrameCache sharedSpriteFrameCache];

[CacheaddSpriteFramesWithFile: @ "Flower. plist"];

Idsheet = [CCSpriteSheet spriteSheetWithFile: @ "Flower.png"

Capacity: 60];

[SelfaddChild: sheet];

// Add the flower Sprite to the scene

FlowerSprite * sprite = [FlowerSprite node];

CGSize size = [[CCDirector shareddire] winSize];

Sprite. position = ccp (size. width/2, size. height/2 );

[SelfaddChild: sprite];

CCSpriteFrameCache * cache = [CCSpriteFrameCache sharedSpriteFrameCache];

NSMutableArray * frames = [[NSMutableArray array] retain];

// Construct the actual image data of each frame

For (int I = 1; I <= FLOWER_SPRITE_SHEET_CAPACITY; I ++ ){

NSString * frameName = [NSStringstringWithFormat: @ "flower1_04d.png", I];

CCSpriteFrame * frame = [cache spriteFrameByName: frameName];

[FramesaddObject: frame];

}

// Use CCAnimation and CCRepeatForever to construct a repeating Animation

NSString * firstFrameName = [NSStringstringWithFormat: @ "Flower%04d.png", 1];

Idsprite = [CCSprite spriteWithSpriteFrameName: firstFrameName];

CCAnimation * animation = [CCAnimation animationWithName: @ "Flower" delay: 1.0f/30 frames: frames];

[SpriterunAction: [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation: animation restoreOriginalFrame: NO];

// Add the constructed animation to the display list

[Self addchild: SPRITE];

Playing sound

With cocos2d simpleaudioengine, you can easily play background music and sound effects.

1. Preparations

Introduce the header file: # import "simpleaudioengine. H"

2. Play background music

[[Simpleaudioengine sharedengine] playbackgroundmusic: @ "background.wav"];

3. Play Sound Effects

[[Simpleaudioengine sharedengine] playeffect: @ "effect1.wav"];

4. Pause background music.

[[Simpleaudioengine sharedengine] pausebackgroundmusic];

5. Pre-load background music

[[Simpleaudioengine sharedengine] preloadbackgroundmusic];

Use cdaudiomanager to change the attributes of Music (before changing the attributes, background music must be played)

6. Change the volume (from 0 to 1)

[Cdaudiomanager sharedmanager]. backgroundmusic. volume = 1.0f;

7. Loop playback of background music for n times

[CDAudioManager sharedManager]. backgroundMusic. numberOfLoops = N;

8. triggered when the background music is stopped.

[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener: self

Selector: @ selector ()];

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.