Cocos2d-x 2.1rc0 OS X 10.8, XCode 4.6.2
Playing around with the HellowWorld with Box2D example to gain some concepts.
Creating an class that is an extension of CCLayerColor.
Previously, before I created a separate object I was doing:
// background
CCLayerColor *background = CCLayerColor::create(cGhostWhite);
background->setContentSize(CCSizeMake(1024, 768));
background->setPosition(0,0);
this->addChild(background,0);
This worked. After trying to create my own object I am getting and error:
error: no viable conversion from 'PlainBackgroundLayer::PlainBackgroundLayer' to 'PlainBackgroundLayer::PlainBackgroundLayer *'
Here is what I am doing:
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);
What am I doing wrong? I feel like I am doing this correctly.
UPDATE 1: now I am doing:
in .cpp:
static PlainBackgroundLayer* PlainBackgroundLayer::create(cocos2d::ccColor3B inColor)
{
// create functions should return autoreleased objects.
PlainBackgroundLayer* layer = new PlainBackgroundLayer();
layer->setColor(inColor);
return layer->autorelease();
}
in .h:
class PlainBackgroundLayer : public cocos2d::CCLayerColor
{
public:
static PlainBackgroundLayer* create(cocos2d::ccColor3B &var);
virtual void draw();
};
and I am getting errors in the .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 *'`