COCOS2D中对精灵的操作

关于精灵的各种操作,总结一下以便以后复习查找。


内容简要:


1、初始化
2、创建无图的精灵
3、设置精灵贴图大小
<wbr style="">4、<span style="margin:0px; padding:0px">添加入层中</span></wbr>


5、对精灵进行缩放
<wbr style="">6、对精灵款或高进行缩放<wbr style="">7、旋转精灵</wbr></wbr>


8、设置精灵透明度
<wbr style="">9、精灵的镜像反转<wbr style="">10、设置精灵的颜色</wbr></wbr>


11、得到图的宽高
<wbr style="">12、按照像素设定图片大小<wbr style="">13、在原有的基础上加xy的坐标</wbr></wbr>


14、设置图片锚点<wbr style=""><wbr style="">15、从新排列z轴顺序<br style=""><wbr style="">16、<span style="margin:0px; padding:0px; white-space:pre">更换</span>精灵<span style="margin:0px; padding:0px; white-space:pre">贴图</span></wbr></wbr></wbr>


17、设置可视区域







原文地址:http://blog.csdn.net/dingkun520wy/article/details/6976558 <wbr style=""><wbr style=""><br style="margin:0px; padding:0px"><br style=""></wbr></wbr>


------------------------------------------------------------------------------------------------------------------------------------------


//初始化


CCSprite*
sprite =[CCSprite spriteWithFile:@"Icon.png"];


<wbr style="">//创建无图的精灵</wbr>


CCSprite*sprite2
=[CCSprite node];


//设置精灵贴图大小
sprite2.textureRect=CGRectMake(0,
0, 20, 20);//设置其为宽20,高20.


//添加入层中


[self
addChild:sprite<wbr style="">z:2]; //将精灵加入层中设置其z轴为2</wbr>


//对精灵进行缩放


sprite.scale=2;//放大2倍


//对精灵款或高进行缩放


sprite.scaleX
= 2;//宽放大2倍<wbr style=""></wbr>


sprite.scaleY
= 2;//高放大2倍


//旋转精灵


sprite.rotation=90;//旋转90度


//设置精灵透明度


sprite.opacity=255;//设置透明度为完全不透明(范围0~255)


//定义精灵位置


sprite.position=ccp(100,100);//设置精灵中心点坐标是x=100,y=100


//精灵的镜像反转


[sprite
setFlipX:YES];//X轴镜像反转


[sprite
setFlipY:YES];//Y轴镜像反转


//设置精灵的颜色


[sprite
setColor:ccc3(255, 0, 0)];//设置颜色为红色


//得到图的宽高


<wbr style=""></wbr>


float
<wbr style=""><span style="margin:0px; padding:0px; white-space:pre">contentSize</span><wbr style=""><wbr style="">=<wbr style="">sprite<wbr style="">.</wbr></wbr></wbr></wbr></wbr>contentSize.width
//得到图片的宽高


//按照像素设定图片大小


<wbr style=""></wbr>


sprite.scaleX=(20)/contentSize;
//按照像素定制图片宽高


//在原有的基础上加xy的坐标


sprite.position
= ccpAdd(sprite.position,ccp(20,20));//在原有坐标的基础上加减坐标


//设置图片锚点


[sprite<wbr style="">setAnchorPoint:ccp(0.5,0.5)<wbr style="">];//设置图片的锚点</wbr></wbr>


//从新排列z轴顺序


[self
reorderChild:sprite z:1];//从新排列z轴顺序


//更换精灵贴图


CCTexture2D * test=[[CCTextureCache sharedTextureCache] addImage:
@"test.png"];//新建贴图


[sprite<wbr style="">setTexture:test];<wbr style=""></wbr></wbr>


//更换精灵贴图,加载帧缓存,这个test.plist保存了fram这张图


[[CCSpriteFrameCache
sharedSpriteFrameCache] addSpriteFramesWithFile:@"test.plist"];


CCSpriteFrame*
frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"fram.png"];
[sprite2
setDisplayFrame:frame];


<wbr style=""></wbr>


//设置可视区域
CCSprite
* sprite3 =[CCSprite spriteWithFile:@"icon.png" rect:CGRectMake(0, 0,
20,20)];//创建时设置


[sprite3
setTextureRect:CGRectMake(10, 10, 30, 30)];//创建后设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值