Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload