0

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 *'`
4

2 回答 2

0

尝试:

PlainBackgroundLayer *background = new PlainBackgroundLayer(cGhostWhite);
this->addChild(background,0);
于 2013-05-06T04:37:50.197 回答
0

请注意,您都是 CCLayerColor 的子类,并且您还有一个名为“background”的成员变量,它是 CCLayerColor。我怀疑您可能误解了继承的工作原理。通常你想要一个或另一个。如果是这种情况,您可能会查看“是”与“有”的关系。

最重要的是,您实际上并没有将“背景”成员变量添加到场景中,因此它不会产生任何影响。此外,您不应该删除基于 CCNode 的对象,甚至不需要对大多数对象调用 release,因为它们通常是由场景自动释放和管理的。

您可能想要做的是删除背景成员变量,并像这样定义一个新的创建方法,并使用一个什么都不做的构造函数。(注意:我没有测试过这段代码):

// 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);
于 2013-05-06T08:24:35.690 回答