您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x学习笔记

2012-11-29 10:59 190 查看
一、CCDirector

CCDirector的主要作用是管理scene。

主要功能如下:

1,switching scenes

2,setting the desired FPS

3,setting the device orientation

4,initializing OpenGL ES

cocos2d-x中目前只有CCDisplayLinkDirector

DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display.



Features and Limitations:

- Scheduled timers & drawing are synchronizes with the refresh rate of the display

- Only supports animation intervals of 1/60 1/30 & 1/15

二、AppDelegate

The AppDelegate is the main entry point of your application.

三、CCNode

anchorPoint: affect how the objects are rotated and scaled.

position: sets the position of the node relative to its parent node.

四、SpriteSheets

a SpriteSheets or Texture atlas is a large image that contains other smaller images. The benefits of using a spritesheet instead of lots of small images is reducing the

texture memory and increasing performance.

The first thing to do when using a spriteSheet is to create a CCSpriteBatchNode object.

The rect containing the small sprites in a spriteSheet can be different.

The sprites selecting a rectangular portion of the spriteSheet need to be added as a child of the spriteSheet, ensuring that all the sprites will be rendered in one go.

If a sprite is a child of a spriteSheet we can no longer change the texture, we now have to change the RECT to match another portion of the spriteSheet.

CCSprite.setTextureRect()

-> CCSprite.setTextureRectInPixels() ---- m_obOffsetPositionInPixels

CCSpriteBatchNode.draw()

->CCSprite.updateTransform()

using spriteSheets have some limitations:

It only accepts CCSprites as a child;

All its children are either aliased or antialiased;

loading three or four spriteSheets into memory will probably cause you trouble;

The Z-order of each CCSprite inside a spriteSheet is relative to it.

The plist file contains the definitions for each image inside the spriteSheet we included.



This plist contains the following two keys:

Texture: Contains the width and height of the whole spriteSheet;

Frames: This has a key for each iamge included in the spriteSheet.

五、action

Position actions: CCMoveTO , CCMoveBy , CCJumpTo , CCJumpBy , CCBezierTo , CCBezierBy , CCPlae

Sacle actions: CCScaleTo , CCScaleBy

Rotation actions: CCRotateTo , CCRotateBy

Visible actions: CCShow , CCHide , CCToggleVisibility , CCBlink

Opacity actions: CCFadeIn , CCFadeOut , CCFadeTo

Color actions: CCTintTo , CCTintBy

CCSpawn lets you run many actions at the same time.

CCSequence allows you to run several actions on after another.

CCRepeat lets you repeat an action a limited amount of times.

CCRepeatForever lets you repeat an action forever.

Ease actions are special composition actions that let you modify the time ot the inner action.

They modify the speed of the inner action, but not the time it takes to perform.

Effect actions modiy the node's Grid. This is a new property that divide's the node into smaller squares or tiles, allowing you to modify the node through them through them by moving the vertices that compose each square. There are two types of grids, namely,
tiled and non-tiled. The difference is that the non-tiled grid is composed by vertex, and the tiled one is composed of tiles, each with its individual vertex.



CCWaves, CCWaves3D, CCFlipX3D, CCFlipY3D, CCLens3D, CCRipple3D, CCShaky3D, CCLiquid, CCTwirl

Some of these effects end in "3D", such as CCShaky3D, or CCRipple3D. These are effects that modify each vertex's Z coordinate, giving a nice three dimensional effect.

CCStopGrid does not take any parameter. What it does is return every vertex of the grid to their original position, thus "restarting" the image to its original form before the effect is applied.

cocos2d doesn't allow a single action to be run on multiple nodes at the same time, so if you want to do something like this, use CCAction.copy() to make a copy of the CCAction object.

special actions: CCCallFunc, CCCallFuncN, CCCallFuncND

animation

to animate a sprite is to run a sequence of images fast enough to give the illusion of movement.

Animations are made of multiple frames, each one displaying different images.

CCSpriteFrames are just objects that hold information about the Rect that makes the frame from a Spritesheet. These frames will later be taken by the CCAnimation class to compose the animations.

CCSpriteFrameCache class is a singleton that handles the loading of sprite frames and saves them in a cache for later use.

You can use the .plist file to load all the frames in the Spritesheet and use them for your animations.

for example: CCSpriteFrameCache.sharedSpriteFrameCache.addSpriteFramesWithFile("coloredSheet.plist");

CCSpriteFrame *frame = CCSpriteFrameCache.sharedSpriteFrameCache.spriteFrameByName("sBlue5.png");

六、Label

Labels are used to add text to your game that can be transformed and moved around as you please. You can even apply actions to them to animate them.

As CCLabels are very slow to render, you should use them with causion. Don't go around changing them in every frame because you will have some performance issues in the long run. You can use the other types of labels(CCLabelAtlas, CCBitmapFontAtlas) for
that.

CCLabelAtlases are labels that take their characters from an image file. This makes them a lot faster than normal CCLabels. notes: all characters must be of the same fixed size; The characters have to be placed in the same order they appear in the ASCII
table.

CCBitmapFontAtlas(CCLabelBMFont) has the flexibility of CCLabels and the speed of the CCLabelAtlas. Also, there are some nice editors for creating your own BitmapAtlases. When in doubt, you should use this class to render your texts. another feature of CCBitmapFontAtlas
is that it treats each character as an individual CCSprite, if you want to modify or apply actions to them individually or as a whole. the only limitation is that you shouldn't change the anchor point of the characters, as that might affect rendering(you still
can change the anchor point of the whole label to align it).

To use CCBitmapFontAtlas, you should have .fnt file and its relevant .png file. If you open the .fnt file, you will find some information about the font used and lots of coordinates and sizes which correspond to every character you had in the .png file. These
are used internally by the CCBitmapFontAtlas class to determine the position of each character inside the .png file, where the images are stored. Since retrieving all the definitions from the file takes a little while, sometimes it can get a little slowdown
when creating your CCBitmapFontAtlas labels. So generally it is a good idea to have them precreated at the beginning of the scene and reuse them later.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: