1. 程式人生 > >cocos2d-x 3.0 經常使用對象的創建方式

cocos2d-x 3.0 經常使用對象的創建方式

strong array 5% eat 自己 ++ -a 緩存 print

cocos2d-x 3.0 中全部對象差點兒都能夠用create函數來創建,其它的創建方式也是有create函數衍生。

以下來介紹下create函數創建一般對象的方法,省得開發中常常忘記啥的。

1、精靈Sprite的4種創建方式

(1)依據圖片資源路徑來創建

//依據圖片路徑來創建
auto sprite1 = Sprite::create(filepath);
//依據圖片路徑來創建,並設置要顯示的圖片大小
auto sprite2 = Sprite::create(filepath,Rect(0,0,width,height));

(2)依據plist文件裏的frame name創建,圖片名稱前要加#符號來區分

//參數:幀名字,frame name
auto sprite = Sprite::create(‘#ball.png‘);
(3)依據緩存plist中的sprite frame來創建,這樣的用的比較多
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("loadingAndHP.plist","loadingAndHP.png");//載入圖片資源作為緩存  
//從緩存圖片中依據圖片名字來創建
auto loading_bk=Sprite::createWithSpriteFrameName("loading_bk.png");
(4)依據紋理texture創建
//依據紋理圖片來創建
auto batch = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 1);//載入緩存紋理
//依據紋理來創建精靈
auto sprite = Sprite::createWithTexture(batch->getTexture());
//依據紋理來創建精靈,並設置顯示區域大小
auto sprite = Sprite::createWithTexture(batch->getTexture(), Rect(x, y, width, height));


2、文字LabelTTF的創建方式

(1)依據字體、大小等多參數創建

auto center = LabelTTF::create("hello cocos2d-x",//要顯示字符串
                                "Paint Boy",//字體
                                 32,//字號
                                 Size(s.width/2,200),//顯示的寬和高
                                 TextHAlignment::CENTER,//顯示的位置,定位
                                 TextVAlignment::TOP);
(2)依據自己定義對象FontDefinition來創建
    FontDefinition shadowTextDef;
    shadowTextDef._fontSize = 20;//字號
    shadowTextDef._fontName = std::string("Marker Felt");//字體
    
    shadowTextDef._shadow._shadowEnabled = true;//設置是否有陰影
    shadowTextDef._shadow._shadowOffset  = shadowOffset;
    shadowTextDef._shadow._shadowOpacity = 1.0;//設置透明度
    shadowTextDef._shadow._shadowBlur    = 1.0;
    shadowTextDef._fontFillColor   = tintColorRed;
    
    // shadow only label
    auto fontShadow = LabelTTF::createWithFontDefinition("Shadow Only Red Text", shadowTextDef);//依據自己定義的字體創建要顯示的內容


3、對於3.0中SpriteBatchNode,不鼓舞使用,文檔看這裏點擊打開鏈接,在此不再贅述SpriteBatchNode的使用方法。

4、幀動畫創建方式

通過多張圖片文件來創建一個動畫。

Animation* animation = Animation::create();
animation->setDelayPerUnit(1.0 / fps);
for (int i = 0; i <= count; i++) {

	const char *filename = String::createWithFormat(fmt, i)->getCString();
	SpriteFrame* frame = SpriteFrameCache::getInstance()->spriteFrameByName(
				filename);
	animation->addSpriteFrame(frame);
}
Animation是由Sprite frame幀組、單個frame延時,持續時間等組成的,它是一組“數據”,而Animate是一個Action,它是基於Animation對象創建的。


5、序列幀動畫Sprite sheet animation

(1)通過.plist文件來創建

SpriteFrameCache::getInstance()->addSpriteFramesWithFile(filename);//載入.plist文件到緩存中
AnimationCache* cache = AnimationCache::getInstance()->addAnimationsWithFile(filename);//依據動畫.plist文件來創建動畫緩存
Animation* animation = cache->animationByName(filename);//直接從緩存中創建動畫
Animate* animate = Animate::create(animation);//生成動畫動作

(2)通過數組來創建

Array* animFrames = Array::createWithCapacity(15);
char str[100] = {0};
for(int i = 1; i < 15; i++)
{
	sprintf(str, "grossini_dance_%02d.png", i);
	SpriteFrame* frame = cache->spriteFrameByName( str );
	animFrames->addObject(frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.3f);

好了,這篇簡介了遊戲中常常要用到的對象的創建方式,對於遊戲中的其它元素。能夠參考遊戲源代碼的demo中的創建方式,這個demo值得好好研究學習。

下一篇介紹下遊戲中的設計模式——托付模式




cocos2d-x 3.0 經常使用對象的創建方式