CCSprite的使用方法大全
一、精灵创建及初始化
1、从图片文件创建:
CCSprite *sprite = [CCSprite spriteWithFile:@"ImageFileName.png"];
默认锚点 ccp(0.5,0.5), 默认位置 ccp(0,0), CCSprite尺寸(contentSize)为图片尺寸
2、从帧缓存创建:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"MineSweeping.plist"];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"ImageFileName.png"];
3、初始化及自定义大小
CCSprite *sprite = [CCSprite spriteWithFile:@"ImageFileName.png" rect:CGRectMake(x,y,w,h)];
只显示图片的一部分,大小为 w,h
iOS设备的贴图尺寸必须符合“2的n次方” 规定,所以贴图的宽和高必须是2,4,8,16,32,64,128,256,512,1024. 在第三代设备上可以达到2048像素
二、精灵常用属性及方法:
添加子节点,CCSprite继承自CCNode,可以进行addChild的操作
[self addChild:sprite];
设置CCSprite位置,本地GL坐标系
[s setPosition:ccp(x,y)];
sprite.position=ccp(100,100);//设置精灵左下角坐标是x=100,y=100,本地GL坐标系
缩放 (参数为比例,1保持不变,0.5代表50%,2代表200%)
sprite.scale=2;//放大2倍
旋转
sprite.rotation=90;//旋转90度
设置透明度 (范围0~255)
sprite.opacity=255;//设置透明度为完全不透明(范围0~255)
设置CCSprite锚点,左下角:
sprite.anchorPoint=ccp(0,0);//设置锚点为左下角,默认为ccp(0.5,0.5)中心点
开启CCSprite镜像
[sprite setFlipX:YES];//X轴镜像反转
[sprite setFlipY:YES];//Y轴镜像反转
是否可见
[sprite setVisible:NO];//设置隐藏,默认为可见
设置CCSprite(图片)的颜色
[sprite setColor:ccc3(255, 0, 0)];//设置颜色为红色,三原色
CCSprite的层叠次序 (次序小的在下面,大的在上面)
[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或者CCNode
五、精灵换图
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];
六、移除Sprite:
-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
}
七、精灵批处理(Sprite Batching):
创建多个CCSprite节点,将它们添加到同一个CCSpriteBatchNode中以提高渲染速度
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];
[self addChild:batch];
for (int i = 0; i < 100; i++)
{
CCSprite* sprite = [CCSprite spriteWithFile:@"bullet.png"];
[batch addChild:bullet];
}
CCSprite* sprite = [CCSprite spriteWithFile:@"bullet.png"];
[batch addChild:bullet];
什么时候应该使用CCSpriteBatchNode
当你需要显示两个或者更多个相同的CCSprite节点时,你可以使用CCSpriteBatchNode。组合在一起的CCSprite节点越多,使用 CCSpriteBatchNode得到的效果提升就越大
出处:http://www.cocos2dchina.com/archives/323#more-323
CCSprite的使用方法大全的更多相关文章
- [OC][转]UITableView属性及方法大全
Tip: UITableView属性及方法大全 (摘录地址) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 1 ...
- JS去掉首尾空格 简单方法大全(原生正则jquery)
JS去掉首尾空格 简单方法大全 var osfipin= ' http://www.cnblogs.com/osfipin/ '; //去除首尾空格 osfipin.replace(/(^\s*)|( ...
- js跳转页面方法大全
js跳转页面方法大全<span id="tiao">3</span><a href="javascript:countDown"& ...
- Java获取各种常用时间方法大全
Java获取各种常用时间方法大全 package cc.javaweb.test; Java中文网,Java获取各种时间大全 import java.text.DateFormat; import j ...
- php文件上传大小限制的修改方法大全
php文件上传大小限制的修改方法大全 基本就是修改maxsize选项,当然为了提高上传文件的成功率,还需要设置超时时间等. 文章如下: [php文件上传]php文件上传大小限制修改,phpmyadmi ...
- [Java]读取文件方法大全(转)
[Java]读取文件方法大全 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** ...
- 网站开发进阶(二十六)js刷新页面方法大全
js刷新页面方法大全 在项目开发过程中,需要实现刷新页面.经过学习,发现下面这条语句就可以轻松实现. location.reload(); // 刷新页面 有关刷新页面的其它方法,具体学习内容如下,有 ...
- JavaScript数组方法大全(推荐)
原网址:http://www.jb51.net/article/87930.htm 数组在笔试中经常会出现的面试题,javascript中的数组与其他语言中的数组有些不同,为了方便之后数组的方法学习, ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
随机推荐
- MSChart参考
MSChart在vs2008中使用遇到一个问题,坐标轴的标题为中文时被图表区域遮挡了一部分. 解决办法:在说明文字前加\n实现换一行显示. //this.Chart1.ChartAreas[0].Ax ...
- PushKit和传统长连接方式的比较
iOS and PushKit This post will cover basic concepts for VoIP apps on iOS. I will not post any code ( ...
- The Beginner’s Guide to iptables, the Linux Firewall
Iptables is an extremely flexible firewall utility built for Linux operating systems. Whether you’re ...
- [LeetCode]题解(python):107 Binary Tree Level Order Traversal II
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...
- @JsonProperty的使用
jackson的maven依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <art ...
- 另一个SqlParameterCollection中已包含SqlParameter
一般情况下,我们定义的一个SqlParameter参数数组,如: SqlParameter[] parms = { new SqlParamete ...
- Oracle deadlock SX/SSX caused by no index on foreign key.
Example to show the dead lock caused by lack of index on foreign key of child table. Session 1: crea ...
- rpyc 回调模式工作不正常
rpyc 回调模式工作不正常 最近使用了 rpyc 来处理一个多节点间的文件同步的任务,目标是使用 rpyc 来实现简单的 p2p 文件传输机制,以减少单点负载和单点失败对传输的影响. 和 p2p 的 ...
- python_模块
1. 模块的导入 (1) python中import module时,系统通常在哪些路径下面查找模块? 在以下的路径查找模块:sys.path 如果你模块所在的目录,不在sys.path的目录下,可以 ...
- javascript设计模式学习之十三——职责链模式
一.职责链的定义和使用场景 职责链模式的定义是,职责链模式将一系列可能会处理请求的对象连接成一条链,请求在这些对象之间一次传递,直到遇到一个可以处理它的对象.从而避免请求的发送者和接收者之间的耦合关系 ...