code-prettify

顯示具有 cocos2d-x 標籤的文章。 顯示所有文章
顯示具有 cocos2d-x 標籤的文章。 顯示所有文章

2015年6月9日 星期二

Cocos2d-x 文章列表


Cocos2d-x 3.2 遊戲範例

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series
Chapter 1 - How to Create a New cocos2d-x project
Chapter 2 - How to Add a sprite
Chapter 3 - How to Move a sprite
Chapter 4 - How to Fire some Bullets
Chapter 5 - How to Detect the Collisions
Chapter 6 - How to Play Music and Sound Effect
Chapter 7 - Some Icing on the Cake

Cocos2d-x 3.0 遊戲範例

Cocos2d-x 3.0 - Step by Step Cocos2dxSimpleGame Series
Chapter 1 - How to Create a New cocos2d-x project
Chapter 2 - How to Add a sprite
Chapter 3 - How to Move a sprite
Chapter 4 - How to Fire some Bullets
Chapter 5 - How to Detect the Collisions
Chapter 6 - How to Play Music and Sound Effect
Chapter 7 - Some Icing on the Cake

從 2.x 版本升級為 3.x 版本

Cocos2d-x 2.x to 3.x

2014年10月30日 星期四

Cocos2d-x 3.2 - Chapter 6 - How to Play Music and Sound Effect

Cocos2d-x 3.2 - Chapter 6 - How to Play Music and Sound Effect

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

接著上一篇 Cocos2d-x 3.2 - Chapter 5 - How to Detect the Collisions

在這個章節,我們加入背景音樂及發射子彈時的聲音。

在 HelloWorldScene.cpp 加入 include SimpleAudioEngine.h


#include "SimpleAudioEngine.h"
然後在 init 函式裡面加上這段程式碼,撥放背景音樂。


CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(
  "audio/background-music-aac.wav", true);
然後在 addProjectile函式裡面加上這段,撥放子彈音效。

CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("audio/pew-pew-lei.wav");

參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

2014年10月16日 星期四

Cocos2d-x 3.2 - Chapter 5 - How to Detect the Collisions

Cocos2d-x 3.2 - Chapter 5 - How to Detect the Collisions

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

接著上一篇 Cocos2d-x 3.2 - Chapter 4 - How to Fire some Bullets
我們的英雄現在會發射子彈了,但是子彈只有視覺效果,要怎麼殺死敵人呢?

在這個章節,我們會介紹並實現碰撞。

我們將 Sprites 分成兩類,tag = 1 的是敵人,tag = 2 的是子彈。因為 Sprite 類別是繼承 Node 類別,已經有一個成員變數 _tag 及兩個用來操作的函式 getTag()、setTag()。我們可以直接使用他們來區分兩種 Sprites。

在 HelloWorldScene.h 加兩個成員變數


protected:
 cocos2d::Vector<cocos2d::Sprite *> _targets;
 cocos2d::Vector<cocos2d::Sprite *> _projectiles;


然後針對兩個變數在建構式及解構式作處理


HelloWorld::HelloWorld()
{

}

HelloWorld::~HelloWorld()
{
 for (auto target : this->_targets)
 {
  this->removeChild(target, true);
 }

 this->_targets.clear();

 for (auto projectile : this->_projectiles)
 {
  this->removeChild(projectile, true);
 }

 this->_projectiles.clear();
}


在 addTarget() 將新產生的 target 加到 vector


 target->setTag(1);
 _targets.pushBack(target);


同樣在 addProjectile() 將新產生的 projectile 加到 vector


 projectile->setTag(2);
 _projectiles.pushBack(projectile);


然後,將 spriteMoveFinished() 修改為下列的樣子,將 sprites 從 vecotr 移除


 Sprite *sprite = (Sprite *)sender;
 this->removeChild(sprite, true);

 if (sprite->getTag() == 1)  // target
 {
  _targets.eraseObject(sprite);
 }
 else if (sprite->getTag() == 2) // projectile
 {
  _projectiles.eraseObject(sprite);
 }


新增一個 update() 函式,用來偵測是否發生碰撞,移除碰撞的敵人及子彈。
函式的內容如下


void HelloWorld::update(float dt)
{
 cocos2d::Vector<cocos2d::Sprite *> targetsToDelete;
 cocos2d::Vector<cocos2d::Sprite *> projectilesToDelete;

 for (auto projectile : this->_projectiles)
 {
  Rect projectileRect = Rect(
   projectile->getPosition().x - (projectile->getContentSize().width / 2),
   projectile->getPosition().y - (projectile->getContentSize().height / 2),
   projectile->getContentSize().width,
   projectile->getContentSize().height);

  for (auto target : this->_targets)
  {
   Rect targetRect = Rect(
    target->getPosition().x - (target->getContentSize().width / 2),
    target->getPosition().y - (target->getContentSize().height / 2),
    target->getContentSize().width,
    target->getContentSize().height);

   if (projectileRect.intersectsRect(targetRect))
   {
    targetsToDelete.pushBack(target);
    projectilesToDelete.pushBack(projectile);
   }
  }
 }

 for (auto target : targetsToDelete)
 {
  _targets.eraseObject(target);
  this->removeChild(target, true);
 }

 for (auto projectile : projectilesToDelete)
 {
  _projectiles.eraseObject(projectile);
  this->removeChild(projectile, true);
 }
}


最後,在 init() 將 update() 加入排程 (schedule),每個 frame 都會呼叫一次。


this->schedule(schedule_selector(HelloWorld::update));


這章的成果圖跟上一章一樣,但是現在的子彈充滿了力量,哈哈,敵人來一個殺一個啦!

下一章,Cocos2d-x 3.2 - Chapter 6 - How to Play Music and Sound Effect

參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

2014年10月13日 星期一

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

將原先官方 wiki 上的 範例,從 Cocos2d-x 2.x 版本,改為 3.x 版本。
Souse : Step by Step Cocos2dxSimpleGame Series ( for cocos2d-x 2.x)

其中關於 Cocos2d-x 2.x to 3.x 的注意事項,請參考 Cocos2d-x 2.x to 3.x 。

Chapter 1 - How to Create a New cocos2d-x project
Chapter 2 - How to Add a sprite
Chapter 3 - How to Move a sprite
Chapter 4 - How to Fire some Bullets
Chapter 5 - How to Detect the Collisions
Chapter 6 - How to Play Music and Sound Effect
Chapter 7 - Some Icing on the Cake

Cocos2d-x 3.2 - Chapter 4 - How to Fire some Bullets

Cocos2d-x 3.2 - Chapter 4 - How to Fire some Bullets

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

接著上一篇 Cocos2d-x 3.2 - Chapter 3 - How to Move a sprite,我們增加了敵人之後。
接著,我們試著發射子彈來殺死敵人。

首先,先開啟觸控事件,在 init 函式最後加入下列程式碼:


 //Create a "one by one" touch event listener (processes one touch at a time)
 auto listener1 = EventListenerTouchOneByOne::create();

 // When "swallow touches" is true, then returning 'true' from the onTouchBegan method will "swallow" the touch event, preventing other listeners from using it.
 listener1->setSwallowTouches(true);

 // Example of using a lambda expression to implement onTouchBegan event callback function
 listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
 listener1->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);


然後實作 onTouchBegan 及 onTouchEnded 兩個函式:



bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
 return true;
}

void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
 // Choose one of the touches to work with
 Point location = touch->getLocationInView();
 location = Director::getInstance()->convertToGL(location);
 addProjectile(location.x, location.y);
}

最後是 addProjectile 函式



void HelloWorld::addProjectile(float x, float y)

{

 // Set up initial location of projectile

 Size winSize = Director::getInstance()->getWinSize();

 Sprite *projectile = Sprite::create("Projectile.png", Rect(0, 0, 20, 20));

 projectile->setPosition(Point(20, winSize.height / 2));



 // Determinie offset of location to projectile

 int offX = x - projectile->getPosition().x;

 int offY = y - projectile->getPosition().y;



 // Bail out if we are shooting down or backwards

 if (offX <= 0) return;



 // Ok to add now - we've double checked position

 this->addChild(projectile);



 // Determine where we wish to shoot the projectile to

 int realX = winSize.width

  + (projectile->getContentSize().width / 2);

 float ratio = (float)offY / (float)offX;

 int realY = (realX * ratio) + projectile->getPosition().y;

 Point realDest = Point(realX, realY);



 // Determine the length of how far we're shooting

 int offRealX = realX - projectile->getPosition().x;

 int offRealY = realY - projectile->getPosition().y;

 float length = sqrtf((offRealX * offRealX)

  + (offRealY*offRealY));

 float velocity = 480 / 1; // 480pixels/1sec

 float realMoveDuration = length / velocity;



 // Move projectile to actual endpoint

 projectile->runAction(CCSequence::create(

  CCMoveTo::create(realMoveDuration, realDest),

  CCCallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1)), NULL));



 projectile->runAction(CCSequence::create(

  CCMoveTo::create(realMoveDuration, realDest),

  CCCallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1)), NULL));

}

完成圖:



參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

EventDispatcher Mechanism
http://www.cocos2d-x.org/wiki/EventDispatcher_Mechanism

2014年10月5日 星期日

Cocos2d-x 3.2 - Chapter 3 - How to Move a sprite

Cocos2d-x 3.2 - Chapter 3 - How to Move a sprite

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

在上一篇 Cocos2d-x 3.2 - Chapter 2 - How to Add a sprite 中,我們已經增加了一位英雄。但是英雄只有一個人太孤單了,所以我們來增加一些敵人。

addTarget() 這個函式會讓敵人隨機的出現,並從右向左移動。


void HelloWorld::addTarget()
{
 Sprite *target = Sprite::create("Target.png", Rect(0, 0, 27, 40));

 // Determine where to spawn the target along the Y axis
 Size winSize = Director::getInstance()->getWinSize();
 int minY = target->getContentSize().height / 2;
 int maxY = winSize.height - target->getContentSize().height / 2;
 int rangeY = maxY - minY;
 // srand( TimGetTicks() );
 int actualY = (rand() % rangeY) + minY;

 // Create the target slightly off-screen along the right edge,
 // and along a random position along the Y axis as calculated
 target->setPosition(Point(winSize.width + (target->getContentSize().width / 2), actualY));
 this->addChild(target);

 // Determine speed of the target
 int minDuration = (int)2.0;
 int maxDuration = (int)4.0;
 int rangeDuration = maxDuration - minDuration;
 // srand( TimGetTicks() );
 int actualDuration = (rand() % rangeDuration) + minDuration;

 // Create the actions
 FiniteTimeAction* actionMove =
  MoveTo::create((float)actualDuration,
  Point(0 - target->getContentSize().width / 2, actualY));

 FiniteTimeAction* actionMoveDone =
  CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1));

 target->runAction(Sequence::create(actionMove, actionMoveDone, NULL));
}

其中 CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1)) 使用了回呼函式,所以我們需要宣告 spriteMoveFinished 函式在 HelloWorld。


void HelloWorld::spriteMoveFinished(Node* sender)
{
 Sprite *sprite = (Sprite *)sender;
 this->removeChild(sprite, true);
}

接著,我們需要定時讓敵人出現在場景上,加入下列這段程式到 init 函中的 return 之前


this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);

然後實作 gameLogic() 函式


void HelloWorld::gameLogic(float dt)
{
 this->addTarget();
}

好了,所有的工作都完成了,建置然後看看結果吧:



參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

2014年10月4日 星期六

Cocos2d-x 3.2 - Chapter 2 - How to Add a sprite

Cocos2d-x 3.2 - Chapter 2 - How to Add a sprite

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

一、前言
接續前一篇 Cocos2d-x 3.2 - Chapter 1 - How to Create a New cocos2d-x project 產生的專案,如果還沒有專案的話可以先參考上面的連結。如果之前用過 Cocos2d-x 2.x 版本的話,那你可能會想要參考一下 Cocos2d-x 2.x 到 3.x 筆記

在第一篇我們已經建置過整個方案了,習慣上我會將 libcocos2d, libchipmunk, libAudio 三個專案「卸載專案」,避免之後重新建置這三個專案。只留下 MyGame 這個主要專案。




二、準備工作
在開始前,因為 3.2 的 Layer 預設是黑底,所以我們先把他改為白底。先將 HelloWorldScene.h 裡面,HelloWorld 繼承的 Layer 改為 LayerColor,然後將 HelloWorldScene.cpp 裡面的 init 函式的 if ( !Layer::init() ) 改為 if (LayerColor::initWithColor(Color4B(255, 255, 255, 255)) == false)。

if (LayerColor::initWithColor(Color4B(255, 255, 255, 255)) == false)
{
    return false;
}

將背景改為白色的同時,也可以把原本的  Hello World 文字改為黑字,新增這一行。

label->setColor(Color3B::BLACK);

順便把預設的 COCOS2DX 圖片拿掉,免得擋住之後的畫面。
刪除下列程式碼。

auto sprite = Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite, 0);

三、新增 Sprite
在 MyGame 專案中,找到 HelloWorldScene.cpp 中的 init 函式,在 return true 之前加上這一段

Size winSize = Director::getInstance()->getWinSize();
Sprite *player = Sprite::create("Player.png",Rect(0, 0, 27, 40));
player->setPosition(Point(player->getContentSize().width / 2, winSize.height / 2));
this->addChild(player);

最後,我們可以看到一位英雄孤獨的站在白色背景中



下一篇: Cocos2d-x 3.2 - Chapter 3 - How to Move a sprite

參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

Cocos2d-x 3.2 - Chapter 1 - How to Create a New cocos2d-x project

Cocos2d-x 3.2 - Chapter 1 - How to Create a New cocos2d-x project

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

Cocs2d-x v3.2 建置環境需求 (Build Requirements)
------------------
* Mac OS X 10.7+, Xcode 4.6+
* or Ubuntu 12.10+, CMake 2.6+
* or Windows 7+, VS 2012+
* Python 2.7.5

實際測試環境
* Cocos2d-x v3.2 版本。
* Windows 7。
* Vistual Studio 2013 Express for Desktop。
* Ptyhon 2.7.6

步驟
一、下載 並解壓縮 cocos2d-x (這裡使用的是 v3.2),假設解壓縮後的資料夾為 D:/cocos2d-x-3.2。

二、進入 D:/cocos2d-x-3.2/build 資料夾,執行 win32-msvc-2013-x86.bat。等一陣子之後就會開始建置了,然建置過程會比較久一點。

三、進入 D:/cocos2d-x-3.0rc1/tools/cocos2d-console/bin,使用 command line 輸入
「cocos new MyGame -p com.MyCompany.MyGame -l cpp -d D:/MyCompany」
之後會在 D:/MyCompany 產生一個 MyGame 資料夾,一個 Cocos2d-x 專案就產生了!



四、開啟 D:/MyCompany/MyGame/proj.win32/MyGame.sln 後,開始建置。
等待建置完成後執行程式,噹噹!


小結:Cocos2d-x 專案越來越方便了…

番外篇:
由於個人對於 warning 反感,所以先對這個「空專案」進行一下整理。

一、有一些 warning 是因為程式碼字元問題,考慮到不想變動 cocos2d-x 原始碼的情況下,
只好在專案屬性把警告關閉了。
在專案點擊右鍵 > 屬性 > 組態屬性 > C/C++ > 進階 > 停用特定警告
把 4819 加進去就好。

二、另外的警告則是 cocos2d-x 3.0 開始,
CCLabelTTF, CCLabelBMFont and CCLabelAtlas 改用 Label 取代,
將原本的程式碼改用這一行取代,
然後將字型複製到資料夾 MyGame/Resources/fonts 底下。

auto label = Label::createWithTTF("Hello World", "fonts/arial.ttf", 24);

重新建置確認一下,沒有任何警告訊息,雖然只是個人的小小堅持,
但是就怕長久習慣警告的出現,對於真正的警告出現時,就有可能忽略他,
所以還是調整了一下。

下一篇: Cocos2d-x 3.2 - Chapter 2 - How to Add a sprite

參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

How To Start A New Game (New Version for v3.0 rc1)
https://github.com/chukong/cocos-docs/blob/master/manual/framework/native/getting-started/v3.0/how-to-start-a-new-game/en.md

How to create a new game (Old Version for cocos2d-x v3.0-alpha0 ~ v3.0 beta2)
http://www.cocos2d-x.org/wiki/How_to_create_a_multi-platform_project_in_one_command_line

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

2014年4月18日 星期五

Cocos2d-x 3.0 - Chapter 3 - How to Move a sprite

Cocos2d-x 3.0 - Chapter 3 - How to Move a sprite
Cocos2dxSimpleGame Series

在上一篇 Chapter 2 - How to Add a sprite 中,我們已經增加了一位英雄。但是英雄只有一個人太孤單了,所以我們來增加一些敵人。

addTarget() 這個函式會讓敵人隨機的出現,並從右向左移動。


void HelloWorld::addTarget()
{
 Sprite *target = Sprite::create("Target.png", Rect(0, 0, 27, 40));

 // Determine where to spawn the target along the Y axis
 Size winSize = Director::getInstance()->getWinSize();
 int minY = target->getContentSize().height / 2;
 int maxY = winSize.height - target->getContentSize().height / 2;
 int rangeY = maxY - minY;
 // srand( TimGetTicks() );
 int actualY = (rand() % rangeY) + minY;

 // Create the target slightly off-screen along the right edge,
 // and along a random position along the Y axis as calculated
 target->setPosition(Point(winSize.width + (target->getContentSize().width / 2), actualY));
 this->addChild(target);

 // Determine speed of the target
 int minDuration = (int)2.0;
 int maxDuration = (int)4.0;
 int rangeDuration = maxDuration - minDuration;
 // srand( TimGetTicks() );
 int actualDuration = (rand() % rangeDuration) + minDuration;

 // Create the actions
 FiniteTimeAction* actionMove =
  MoveTo::create((float)actualDuration,
  Point(0 - target->getContentSize().width / 2, actualY));

 FiniteTimeAction* actionMoveDone =
  CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1));

 target->runAction(Sequence::create(actionMove, actionMoveDone, NULL));
}

其中 CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1)) 使用了回呼函式,所以我們需要宣告 spriteMoveFinished 函式在 HelloWorld。


void HelloWorld::spriteMoveFinished(Node* sender)
{
 Sprite *sprite = (Sprite *)sender;
 this->removeChild(sprite, true);
}

接著,我們需要定時讓敵人出現在場景上,加入下列這段程式到 init 函中的 return 之前


this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);

然後實作 gameLogic() 函式


void HelloWorld::gameLogic(float dt)
{
 this->addTarget();
}

好了,所有的工作都完成了,建置然後看看結果吧:



參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series


Cocos2d-x 3.0 - Step by Step Cocos2dxSimpleGame Series

Cocos2d-x 3.0 - Step by Step Cocos2dxSimpleGame Series

將原先官方 wiki 上的 範例,從 Cocos2d-x 2.x 版本,改為 3.x 版本。
Souse : Step by Step Cocos2dxSimpleGame Series ( for cocos2d-x 2.x)

其中關於 Cocos2d-x 2.x to 3.0 的注意事項,請參考 Cocos2d-x 2.x to 3.0 rc1 Note 。

Chapter 1 - How to Create a New cocos2d-x project
Chapter 2 - How to Add a sprite
Chapter 3 - How to Move a sprite
Chapter 4 - How to Fire some Bullets
Chapter 5 - How to Detect the Collisions
Chapter 6 - How to Play Music and Sound Effect
Chapter 7 - Some Icing on the Cake

2014年4月10日 星期四

Cocos2d-x 3.0 - Chapter 2 - How to Add a sprite

Cocos2d-x 3.0 - Chapter 2 - How to Add a sprite
Cocos2dxSimpleGame Series

一、前言
接續前一篇 Cocos2d-x 3.0 - Chapter 1 - How to Create a New cocos2d-x project 產生的專案,如果還沒有專案的話可以先參考上面的連結。如果之前用過 Cocos2d-x 2.x 版本的話,那你可能會想要參考一下 Cocos2d-x 2.x to 3.0 rc1 Note

在第一篇我們已經建置過整個方案了,習慣上我會將 libcocos2d, libchipmunk, libAudio 三個專案「卸載專案」,避免之後重新建置這三個專案。只留下 MyGame 這個主要專案。




二、背景
在開始前,因為 3.0 的 Layer 預設是黑底,所以我們先把他改為白底。先將 HelloWorldScene.h 裡面,HelloWorld 繼承的 Layer 改為 LayerColor,然後將 HelloWorldScene.cpp 裡面的 init 函式的 if ( !Layer::init() ) 改為 if (LayerColor::initWithColor(Color4B(255, 255, 255, 255)) == false)。


if (LayerColor::initWithColor(Color4B(255, 255, 255, 255)) == false)
{
    return false;
}

三、新增 Sprite
在 MyGame 專案中,找到 HelloWorldScene.cpp 中的 init 函式,在 return true 之前加上這一段

Size winSize = Director::getInstance()->getWinSize();
Sprite *player = Sprite::create("Player.png",Rect(0, 0, 27, 40));
player->setPosition(Point(player->getContentSize().width / 2, winSize.height / 2));
this->addChild(player);

最後,我們可以看到一位英雄孤獨的站在白色背景中


參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

Cocos2d-x 2.x 到 3.x 筆記

Cocos2d-x 2.x to 3.x Note
Cocos2d-x 2.x 到 3.x 筆記

一、命名規範跳脫原本 Cocos2d 的 Object-C 規範,更接近 C++ 規範,像是去除 CC 前贅字。
Ex:
CCSize => Size
CCDirector => Director
CCSprite => Sprite
CCRectMake => Rect
ccp => Point

二、命名的變更除了類別之後,有些函式也有改變,像是 sharedXXX() 改為 getInstance()。
Ex:
sharedDirector() => getInstance()

三、儘可能使用 std lib,而非自己的函式,
Ex:
CallBackFunction 使用
2.x 寫法:
CCFiniteTimeAction* actionMoveDone =
CCCallFuncN::create( this, callfuncN_selector(HelloWorld::spriteMoveFinished));
3.0 寫法:
FiniteTimeAction* actionMoveDone =
CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1));

四、CCLabelTTF, CCLabelBMFont and CCLabelAtlas 改用 Label 取代

參考資料:
EventDispatcher Mechanism
http://www.cocos2d-x.org/docs/manual/framework/native/input/event-dispatcher/en

cocos2d-x 3.0 beta: the new Vector class
http://dev.bunnyhero.org/2014/01/cocos2d-x-30-beta-the-new-vector-class/

Improved Label in Cocos2d-x-3.0
http://www.cocos2d-x.org/docs/manual/framework/native/v3/label/en

2014年4月8日 星期二

Cocos2d-x 3.0 - Chapter 1 - How to Create a New cocos2d-x project

Cocos2d-x 3.0 - Chapter 1 - How to Create a New cocos2d-x project
Cocos2dxSimpleGame Series

Cocs2d-x v3.0 rc1 建置環境需求 (Build Requirements)
------------------
* Mac OS X 10.7+, Xcode 4.6+
* or Ubuntu 12.10+, CMake 2.6+
* or Windows 7+, VS 2012+
* Python 2.7.5

實際測試環境
* Cocos2d-x v3.0rc1 版本。
* Windows 7。
* Vistual Studio 2013 Express for Desktop。
* Ptyhon 2.7.6

步驟
一、下載 並解壓縮 cocos2d-x (這裡使用的是 v3.0rc1),假設解壓縮後的資料夾為 D:/cocos2d-x-3.0rc1。

二、進入 D:/cocos2d-x-3.0rc1/build 資料夾,開啟 cocos2d-win32.vc2012.sln,因為本來是 Vistual Studio 2012 的專案,所以使用 Vistual Studio 2013 開啟時會出現專案升級畫面,放心的給他按下去。等一陣子之後就會開啟專案了,然後開始建置方案,建置過程會比較久一點。

三、進入 D:/cocos2d-x-3.0rc1/tools/cocos2d-console/bin,使用 command line 輸入
「cocos new MyGame -p com.MyCompany.MyGame -l cpp -d D:/MyCompany」
之後會在 D:/MyCompany 產生一個 MyGame 資料夾,一個 Cocos2d-x 專案就產生了!



四、開啟 D:/MyCompany/MyGame/proj.win32/MyGame.sln 後,開始建置。
等待建置完成後執行程式,噹噹!


小結:Cocos2d-x 專案越來越方便了…

下一篇: Cocos2d-x 3.0 - Chapter 2 - How to Add a sprite

參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

How To Start A New Game (New Version for v3.0 rc1)
https://github.com/chukong/cocos-docs/blob/master/manual/framework/native/getting-started/v3.0/how-to-start-a-new-game/en.md

How to create a new game (Old Version for cocos2d-x v3.0-alpha0 ~ v3.0 beta2)
http://www.cocos2d-x.org/wiki/How_to_create_a_multi-platform_project_in_one_command_line

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series