Cocos2d-x creating an object based upon CCLayerColor

时光怂恿深爱的人放手 提交于 2020-01-06 21:06:03

问题


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

回答1:


Notice that you are both subclassing CCLayerColor, and you also have a member variable called 'background' that is a CCLayerColor. I suspect you might be misunderstanding how inheritance works. Usually you want one or the other. You might look at 'is a' vs 'has a' relationships if this is the case.

On top of this, you don't actually add the 'background' member variable to the scene, so it will have no effect. Also you shouldn't delete CCNode based objects, and you don't even need to call release on most, since they are usually autoreleased and managed by the scene.

What you probably want to do is remove the background member variable, and define a new create method like this, with a constructor that does nothing. (Note:I haven't tested this code):

// 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()
{

}

The advantage of inheritance is that you can treat derived classes in a similar fashion. Now you can instantiate and add to the scene the same way you did before:

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



回答2:


try:

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


来源:https://stackoverflow.com/questions/16391953/cocos2d-x-creating-an-object-based-upon-cclayercolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!