Cocos2d-x基于CCLayerColor创建对象

时间:2013-05-06 04:26:30

标签: cocos2d-iphone cocos2d-x

Cocos2d-x 2.1rc0 OS X 10.8,XCode 4.6.2

使用Box2D示例使用HellowWorld获取一些概念。

创建一个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;   
}

并实例化如:

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

我做错了什么?我觉得我这样做是正确的。

更新1:现在我在做:

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();   
}

在.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 *'`

2 个答案:

答案 0 :(得分:0)

尝试:

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

答案 1 :(得分:0)

请注意,您既是CCLayerColor的子类,也有一个名为' background'的成员变量。这是一个CCLayerColor。我怀疑你可能误解了继承是如何工作的。通常你想要一个或另一个。你可能会看到'是一个' vs'有一个'如果是这种情况,那就是关系。

除此之外,您实际上并没有添加背景'成员变量到场景,所以它将没有任何效果。此外,您不应该删除基于CCNode的对象,并且您甚至不需要在大多数情况下调用发布,因为它们通常是自动释放并由场景管理。

您可能想要做的是删除后台成员变量,并使用不执行任何操作的构造函数定义这样的新create方法。 (注意:我还没有测试过这段代码):

// 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);