天天看點

Cocos2d 中對圖檔的各種操作

關于精靈的各種操作,總結一下以便以後複習查找。

内容簡要:

1、初始化 2、建立無圖的精靈 3、設定精靈貼圖大小  4、添加入層中

5、對精靈進行縮放  6、對精靈款或高進行縮放  7、旋轉精靈

8、設定精靈透明度  9、精靈的鏡像反轉  10、設定精靈的顔色

11、得到圖的寬高   12、按照像素設定圖檔大小  13、在原有的基礎上加xy的坐标

14、設定圖檔錨點    15、從新排列z軸順序   16、更換精靈貼圖

17、設定可視區域

//初始化

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

 //建立無圖的精靈

CCSprite*sprite2 =[CCSprite node];

//設定精靈貼圖大小

sprite2.textureRect=CGRectMake(0, 0, 20, 20);//設定其為寬20,高20.

//添加入層中

[self addChild:sprite z:2]; //将精靈加入層中設定其z軸為2

//對精靈進行縮放

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

//對精靈款或高進行縮放

sprite.scaleX = 2;//寬放大2倍 

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)];//設定顔色為紅色

//得到圖的寬高

float  contentSize  = sprite .contentSize.width //得到圖檔的寬高

//按照像素設定圖檔大小

sprite.scaleX=(20)/contentSize; //按照像素定制圖檔寬高

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

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

//設定圖檔錨點

[sprite setAnchorPoint:ccp(0.5,0.5) ];//設定圖檔的錨點

//從新排列z軸順序

[self reorderChild:sprite z:1];//從新排列z軸順序

//更換精靈貼圖

CCTexture2D * test=[[CCTextureCache sharedTextureCache] addImage:

@"test.png"];//建立貼圖

[sprite setTexture:test]; 

//更換精靈貼圖,加載幀緩存,這個test.plist儲存了fram這張圖

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

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

[sprite2 setDisplayFrame:frame];