Cocos2d-x学习笔记

本文介绍了Cocos2d-x游戏开发的基本概念和技术要点,包括CCDirector的作用及特性、AppDelegate的功能、CCNode的属性使用、SpriteSheets的创建与限制、常见动作效果的实现方式以及Label的种类和性能注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值