2013-05-06 19 views
0

的Cocos2D-X 2.1rc0 OS X 10.8,的XCode 4.6.2的Cocos2D-X創建基於CCLayerColor

與HellowWorld玩弄box2d的例子來獲得的一些概念的對象。

創建一個擴展了CCLayerColor的類。

以前,我創建了一個單獨的對象之前,我在做:

// background 
CCLayerColor *background = CCLayerColor::create(cGhostWhite); 
background->setContentSize(CCSizeMake(1024, 768)); 
background->setPosition(0,0); 
this->addChild(background,0); 

這個工作。試圖創建自己的對象我得到和錯誤後:

error: no viable conversion from 'PlainBackgroundLayer::PlainBackgroundLayer' to 'PlainBackgroundLayer::PlainBackgroundLayer *' 

下面是我在做什麼:

PlainBackgroundLayer.h:

#ifndef __PLAINBACKGROUNDLAYER_H__ 
#define __PLAINBACKGROUNDLAYER_H__ 

#include "cocos2d.h" 
#include "Box2D.h" 

class PlainBackgroundLayer : public cocos2d::CCLayerColor 
{ 

    public: 
     PlainBackgroundLayer(cocos2d::ccColor4B inColor); 
     ~PlainBackgroundLayer(); 

     virtual void draw(); 

    private: 
     cocos2d::ccColor4B backgroundColor; 
     cocos2d::CCSize layerSize; 
     cocos2d::CCLayerColor *background; 
}; 

#endif // __PLAINBACKGROUNDLAYER_H__ 

PlainBackgroundLayer.cpp:

#include "PlainBackgroundLayer.h" 

using namespace cocos2d; 

PlainBackgroundLayer::PlainBackgroundLayer(cocos2d::ccColor4B inColor) 
{ 
    layerSize = CCDirector::sharedDirector()->getWinSize(); 

    backgroundColor = inColor; 

    background = CCLayerColor::create(backgroundColor); 
    background->setContentSize(CCSizeMake(1024, 768)); 
    background->setPosition(0,0); 
} 

PlainBackgroundLayer::~PlainBackgroundLayer() 
{ 
    delete background; 
} 

and instantiating like:

PlainBackgroundLayer::PlainBackgroundLayer *background = PlainBackgroundLayer::PlainBackgroundLayer(cGhostWhite); 
this->addChild(background,0); 

我在做什麼錯?我覺得我正確地做到了這一點。

更新1:我現在做:

中的.cpp

static PlainBackgroundLayer* PlainBackgroundLayer::create(cocos2d::ccColor3B inColor) 
{ 
    // create functions should return autoreleased objects. 
    PlainBackgroundLayer* layer = new PlainBackgroundLayer(); 
    layer->setColor(inColor); 
    return layer->autorelease(); 
} 

在.H:

class PlainBackgroundLayer : public cocos2d::CCLayerColor 
{ 
    public: 
     static PlainBackgroundLayer* create(cocos2d::ccColor3B &var); 

     virtual void draw(); 
}; 

,我湊了的.cpp錯誤:

`Out-of-line definition of 'create' does not match any declaration in 'PlainBackgroundLayer'` 

`'static' can only be specified inside the class definition` 

`Cannot initialize return object of type 'PlainBackgroundLayer *' with an rvalue of type 'cocos2d::CCObject *'` 

回答

0

請注意,您是CCLayerColor的子類,並且您還有一個名爲'background'的成員變量,它是CCLayerColor。我懷疑你可能會誤解繼承是如何工作的。通常你想要一個或另一個。如果情況是這樣,你可能會看'是'與'有'關係。

最重要的是,您實際上並沒有將「背景」成員變量添加到場景中,所以它不起作用。此外,您不應該刪除基於CCNode的對象,並且您甚至不需要在大多數情況下調用發佈,因爲它們通常是由場景自動發佈和管理的。

你可能想要做的是消除背景的成員變量,並這樣定義一個新的創造方法,與什麼都不做的構造函數。 (注:我沒有測試此代碼):

// this goes in your PlainBackgroundLayer.h's public method section. 
static PlainBackgroundLayer* create(ccColor3B &var); 

// in your cpp: (EDIT: removed static keyword, and make all instances set size/pos) 
PlainBackgroundLayer* PlainBackgroundLayer::create(ccColor3B &var) 
{ 
    // create functions should return autoreleased objects. 
    PlainBackgroundLayer* layer = new PlainBackgroundLayer(); 
    layer->setColor(var); 
    layer->setContentSize(CCSizeMake(1024, 768)); 
    layer->setPosition(0,0); 
    return layer->autorelease(); 

} 

// you can omit this and use default constructor as well. I just want to point out 
// that the constructor doesn't need to do anything 
PlainBackgroundLayer::PlainBackgroundLayer() 
{ 

} 

繼承的好處是,你可以以類似的方式對待派生類。 現在你可以實例,並添加到現場之前,你做的方式一樣:

// background 
PlainBackgroundLayer *background = PlainBackgroundLayer::create(cGhostWhite); 
this->addChild(background,0); 
+0

謝謝你。我更新以反映我收到的更改和一些錯誤。愚蠢的問題,我認爲從一個對象繼承的觀點也是爲了讓這個對象成爲自己足夠的自己。因此,在上面的示例中,我仍然在除PlainBackgroundLayer對象外的其他位置設置contensize(),position()。我誤解了什麼? – Jason 2013-05-06 14:16:28

+0

糟糕,從create()的cpp定義中刪除static關鍵字(我爲此編輯過)。我明白你現在正在做什麼,也爲此更新。 – 2013-05-07 09:33:53

0

嘗試:

PlainBackgroundLayer *background = new PlainBackgroundLayer(cGhostWhite); 
this->addChild(background,0); 
+0

我想到了用「新」的,你能解釋一下,當一個使用「新」,何時不? – Jason 2013-05-06 14:14:21

+0

New創建一個C++類的實例,調用該構造函數並返回指向該實例的指針。它是內存分配的C++語言的標準部分。 「隱藏」這一步的各種create()方法。請記住,在某些時候,您還需要刪除該指針。 – Anonymous 2013-05-07 00:11:50

+0

謝謝澄清,我知道新的,我的意思是什麼時候使用新的與創造()。 – Jason 2013-05-07 14:22:11