Cocos2d 中對圖片的各種操作

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

內容簡要:

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

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

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

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

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

17、設置可視區域 18、貼圖無鋸齒


原文地址:http://blog.csdn.net/dingkun520wy/article/details/6976558

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

//初始化

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];

//設置可視區域
CCSprite * sprite3 =[CCSprite spriteWithFile:@"icon.png" rect:CGRectMake(0, 0, 20,20)];//創建時設置

[sprite3 setTextureRect:CGRectMake(10, 10, 30, 30)];//創建後設置

//貼圖無鋸齒

[sprite3 .texture setAliasTexParameters];



原文地址: http://blog.csdn.net/dingkun520wy/article/details/6976558

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章