的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 *'`
謝謝你。我更新以反映我收到的更改和一些錯誤。愚蠢的問題,我認爲從一個對象繼承的觀點也是爲了讓這個對象成爲自己足夠的自己。因此,在上面的示例中,我仍然在除PlainBackgroundLayer對象外的其他位置設置contensize(),position()。我誤解了什麼? – Jason 2013-05-06 14:16:28
糟糕,從create()的cpp定義中刪除static關鍵字(我爲此編輯過)。我明白你現在正在做什麼,也爲此更新。 – 2013-05-07 09:33:53