Cocos2d-x基于CCLayerColor创建一个对象 [英] Cocos2d-x creating an object based upon CCLayerColor

查看:998
本文介绍了Cocos2d-x基于CCLayerColor创建一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cocos2d-x 2.1rc0
OS X 10.8,XCode 4.6.2



使用HellowWorld和Box2D示例来获得一些概念。 p>

创建一个作为CCLayerColor扩展名的类。



以前,在我创建一个单独的对象之前,我正在做:

  // background 
CCLayerColor * background = CCLayerColor :: create(cGhostWhite);
background-> setContentSize(CCSizeMake(1024,768));
background-> setPosition(0,0);
this-> addChild(background,0);

这工作。尝试创建我自己的对象后,我得到和错误:

 错误:没有可行的转换从'PlainBackgroundLayer :: PlainBackgroundLayer' 'PlainBackgroundLayer :: PlainBackgroundLayer *'

这里是我在做什么:



PlainBackgroundLayer.h:

  #ifndef __PLAINBACKGROUNDLAYER_H__ 
#define __PLAINBACKGROUNDLAYER_H__

#includecocos2d.h
#includeBox2D.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:

  #includePlainBackgroundLayer.h

使用命名空间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:现在我在做:



  static PlainBackgroundLayer * PlainBackgroundLayer :: create(cocos2d :: ccColor3B inColor)
{
// create函数应该返回自动释放的对象。
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();
};

我在.cpp中收到错误:

 `'create'的外联定义与'PlainBackgroundLayer'中的任何声明不匹配

`'static'内部类定义`

`无法初始化类型为'PlainBackgroundLayer *'的返回对象,其类型为'cocos2d :: CCObject *'`
注意,你是CCLayerColor的子类,你还有一个称为'background'的成员变量,它是一个CCLayerColor。我怀疑你可能会误解继承是如何工作的。通常你想要一个或另一个。你可以看看'是一个'vs'有一个'关系,如果是这样的情况。



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



你可能想要做的是删除后台成员变量,并定义一个新的create方法像这样,一个构造函数什么都不做。 (注意:我没有测试这个代码):

  //这在你的PlainBackgroundLayer.h的公共方法部分。 
static PlainBackgroundLayer * create(ccColor3B& var);

//在你的cpp:(编辑:删除静态关键字,并使所有实例设置大小/位置)
PlainBackgroundLayer * PlainBackgroundLayer :: create(ccColor3B& var)
{
//创建函数应该返回自动释放的对象。
PlainBackgroundLayer * layer = new PlainBackgroundLayer();
layer-> setColor(var);
layer-> setContentSize(CCSizeMake(1024,768));
layer-> setPosition(0,0);
return layer-> autorelease();

}

//你可以省略这个,并使用默认构造函数。我只想指出
//构造函数不需要做任何事情
PlainBackgroundLayer :: PlainBackgroundLayer()
{

}

继承的优点是可以类似的方式处理派生类。
现在你可以像以前一样实例化并添加到场景:

  // background 
PlainBackgroundLayer * background = PlainBackgroundLayer :: create(cGhostWhite);
this-> addChild(background,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 *'`

解决方案

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

这篇关于Cocos2d-x基于CCLayerColor创建一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆