cocos2d中CCSprite的使用总结
来源:互联网 发布:sql2000数据库无项目 编辑:程序博客网 时间:2024/05/22 12:35
转自:http://www.linuxidc.com/Linux/2012-08/67910.htm
一、精灵创建及初始化
1、从图片文件创建:
- CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png"];
2、从帧缓存创建:
- [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"MineSweeping.plist"];
- CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"Icon.png"];
3、初始化及自定义大小
- CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png" rect:CGRectMake(x,y,w,h)];
备注:默认锚点ccp(0.5,0.5),默认位置 ccp(0,0),contentSize为精灵图片尺寸
二、精灵常用属性及方法:
- [self addChild:sprite]; //添加入层中,self为CCLayer
- sprite.scale=2;//放大2倍,参数为比例,1保持不变,0.5代表50%,2代表200%
- sprite.rotation=90;//旋转90度
- sprite.opacity=255;//设置透明度为完全不透明(范围0~255)
- sprite.anchorPoint=ccp(0,0);//设置锚点为左下角,默认为ccp(0.5,0.5)中心点
- sprite.position=ccp(100,100);//设置精灵左下角坐标是x=100,y=100,本地GL坐标系
- [sprite setFlipX:YES];//X轴镜像反转
- [sprite setFlipY:YES];//Y轴镜像反转
- [sprite setVisible:NO];//设置隐藏,默认为可见
- [sprite setColor:ccc3(255, 0, 0)];//设置颜色为红色,三原色
- [sprite zOrder]; //精灵层叠次序即Z轴(小的在下面,大的在上面),注意这是只读属性,不能通过sprite.zOrder=2实现Z轴重设
- [sprite setTextureRect:CGRectMake(10, 10, 30, 30)];//起始点坐标(做上角坐标系),宽高
三、添加其他精灵
CCSprite继承自CCNode,所以你可以对它进行addChild的操作:
- CCSprite *s1 = [CCSprite spriteWithFile:@"Icon.png"];
- CCSprite *s2 = [CCSprite spriteWithFile:@"Icon.png"];
- [s1 addChild:s2];
四、精灵Z轴重设
- [self reorderChild:sprite z:10];//self为CCLayer
五、精灵换图
1、直接利用新建贴图进行更换
- //更换贴图
- CCTexture2D * texture =[[CCTextureCache sharedTextureCache] addImage: @"Default.png"];//新建贴图
- [sprite setTexture:texture];
2、利用帧替换
- //加载帧缓存
- [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"MineSweeping.plist"];
- //从帧缓存中取出Default.png
- CCSpriteFrame* frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Default.png"];
- [sprite setDisplayFrame:frame2];
CCSpriteBatchNode的简介与内容
一般游戏图片资源会打包成一张大图,这样节省空间,又提升速度。打包工具有Zwoptex和texturepacker等等。
CCSpriteBatchNode的初始化只要一张图片,也就是那张大图。然后把所有用到那张大图里面的小图的sprite都加到 CCSpriteBatchNode的child,绘制效率就会提高。
1) 缓冲sprite帧和纹理
- 寻找工程目录下面和输入的参数名字一样,但是后缀是.png的图片文件。然后把这个文件加入到共享的CCTextureCache中。
- 解析plist文件,追踪所有的sprite在spritesheet中的位置,内部使用CCSpriteFrame对象来追踪这些信息。
2) 创建一个精灵批处理结点
- 创建一个CCSpriteBatchNode对象,通过传递一个包含所有sprite的batch的名字作为参数,并把它加入到当前场景之中。
- 接下来,你从batch中创建的任何sprite,你应该把它当作CCSpriteBatchNode的一个孩子加进去。只要sprite包含在batch中,那么就没问题,否则会出错。
CCSprite* tableTop = [CCSprite spriteWithSpriteFrameNam e:@"tabletop.png"];
tableTop.position = [Helper screenCenter];
[batch addChild:tableTop]; - CCSpriteBatchNode可以智能地遍历它的所有的孩子结点,并通过一次OpenGL ES call来渲染这些孩子,而不是以前每个sprite都需要一个OpenGL call,这样渲染速度就会更快。
-(CCSpriteBatchNode*) getSpriteBatch
{
}
//Batch是一个CCSpriteBatchNode,下面Batch-》Sprite-》ChildrenSprite,
//它们的Tag分别为:TagofBatch,TagofSprite,TagofChildrenSprite 得到ChildSprite的方法如下,
根据各自的Tag得到相应的数据。
id Batch = [self getChildByTag:TagofBatch];
id Sprite = [Batch getChildByTag:TagofSprite];
CCSprite *ChildrenSprite = (CCSprite*) [Sprite getChildByTag:TagofChildrenSprite];
- cocos2d中CCSprite的使用总结
- cocos2d中CCSprite的使用总结
- cocos2d中CCSprite的使用总结
- cocos2d中CCSprite的使用总结
- Cocos2D中CCSprite注意事项
- cocos2d中的ccsprite使用
- cocos2d-x学习:CCSprite总结
- cocos2d-x学习:CCSprite总结
- cocos2d-x学习:CCSprite总结
- cocos2d-x学习:CCSprite总结
- cocos2d-x中使用CCSprite拼接有缝隙
- cocos2d-x总结CCSprite精灵的加载与常用操作
- cocos2d-x总结 CCSprite精灵的加载与常用操作
- cocos2d中更改CCSprite的饱和度、色相、亮度等属性
- 6.Cocos2d-x中CCSprite的一些记录
- Cocos2d-x中CCSprite的Create流程--h
- Cocos2d-x中使用Lua让CCSprite变灰(Gray)的简单办法,支持递归调用
- CCSprite精灵的使用
- tomcat以https访问
- 什么是SCI
- http头状态信息意议以及在php使用header设置方法
- Socket 与ServerSocket区别
- Android平台锁屏设计说明
- cocos2d中CCSprite的使用总结
- boost::lexical_cast 的使用
- Oracle11g快捷复制数据库
- 内外网同时访问,我的拿来主义
- Uva 10784 - Diagonal
- ucos下用串口做调试口时,接收用户指令的方法
- 相似度法
- 转 win7下android开发环境搭建(win7 64位),32位同理
- (个人原创)zabbix近期监控遇到的一些问题