cocos2d-x 3.0 常用對象的建立方式,cocos2d-x3.0

來源:互聯網
上載者:User

cocos2d-x 3.0 常用對象的建立方式,cocos2d-x3.0

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 nameauto 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值得好好研究學習。

下一篇介紹下遊戲中的設計模式——委託模式





不可以用create-android-projectbat建立cocos2d-x for android項目

應該是沒有適合Android的版本吧!你去Android sdk管理裡面去下載一些Android 2.2,2.3,3.0的版本,然後再運行一下試試。
 
cocos2d-x 30中 auto什意思 詳細點

自動類型變數
比如原來要指定變數是int還是float,現在可以用auto,在賦值的時候,編譯器自動識別類型。
這個是C++11的新標準,你可以參考C++11新標準文檔。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.