Cocos2d-x学习笔记

cocos2d介绍
       cocos2d是一个用来开发2D游戏和基于2D图形的开源游戏引擎,采用的是开源协议BSD,开发者可以放心使用,不必关心授权问题。它是阿根廷人Ricardo Quesada于2008年2月29日发布的,最早使用脚本语言Python来实现的。
它的主要功能如下:

  1. 流程控制(Flow control):非常容易地管理不同场景(scenes)之间的流程控制
  2. 精灵(Sprites):快速而方便的精灵
  3. 动作(Actions):告诉精灵们该做什么。可组合的动作如移动(move)、旋转(rotate)和缩放(scale)等更多
  4. 特效(Effects):特效包括波浪(waves)、旋转(twirl)和透镜(lens)等更多
  5. 平面地图(Tiled Maps):支持包括矩形和六边形平面地图
  6. 转换(Transitions):从一个场景移动到另外一个不同风格的场景
  7. 菜单(Menus):创建内部菜单
  8. 文本渲染(Text Rendering):支持标签和HTML标签动作
  9. 基于Pyglet:没有外部的依赖
  10. 基于OpenGL:支持硬件加速

       Cocos2d-X是一个开源的移动2D游戏框架,MIT许可证下发布的。这是一个C++cocos2d-iPhone项目的版本。cocos2d-X发展的重点是围绕cocos2d跨平台。cocos2d-X提供的框架外,手机游戏,可以写在C+ +或者Lua中,使用API是cocos2d-iPhone完全兼容。cocos2d-X项目可以很容易地建立和运行IOS,Android的三星Bada,黑莓表操作系统和更多。cocos2d-X还支持Windows和Linux,因此,我们可以调试的源代码很容易写在桌面操作系统的编辑。

X有2个含义:1,代表着Cross,即交叉。因为Cocos2D-X为开发者提供了跨平台支持,通过C++语言把游戏逻辑一次编写即可编译到iOS、Android以及更多手机平台上运行;2,标志着该项目是由C + +语言开发的,并提供C + +的API编写的。


一、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. 即矩形的位置参考点,相对于矩形本身的坐标系来说的。假如锚点为(0.5, 0.5),矩形大小为(200,100),则锚点相对于矩形的坐标为(100, 50)。锚点指定了贴图上和所在节点原点(也就是设置位置的点)重合的点的位置,因此只有在C C N o d e 类节点使用贴图的情况下,锚点才有意义。锚点的默认值是(0 . 5 ,0 . 5 ),表示的并不是一个像素点,而是一个乘数因子。(0 . 5 ,0 .5 ) 表示锚点位于贴图长度乘0 . 5 和宽度乘0 . 5 的地方,即贴图的中心。改变锚点的值虽然可能看起来节点的图像位置发生了变化,但其实并不会改变节点的位置,其实变化的只是贴图相对于你设置的位置的相对位置,相当于你在移动节点里面的贴图,而非节点本身。如果把锚点设置成(0 ,0 ),贴图的左下角就会和节点的位置重合,这可能使得元素定位更为方便,但会影响到元素的缩放和旋转等一系列变换。

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.

 

七、CCRect:不同情况下坐标系不同。需要注意。

1、Sprite->setTextureRect(const cocos2d::CCRect &rect);

CCSpriteFrame::frameWithTexture(CCTexture2D *pobTexture, const CCRect& rect)

rect是指定你要显示的矩形区域,可以使用CCRectMake(x, y, width, height)来创建,
x 显示区域的起始点的x
y 显示区域的起始点的y
width 区域宽
height 区域高
要注意的是设置这个显示区域所使用的坐标系是跟cocos2d-x的系统坐标系是有区别的,这个坐标系使用的一般屏幕坐标系,也就是以左上角为原点(0,0)。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值