Maxxx
March 23, 2014, 2:11am
#1
In Beta 2 the node draw
virtual void draw();
In RC0 it has changed to
virtual void draw() final;
So now to override the draw method I need to override the
virtual void draw(Renderer *renderer, const kmMat4& transform, bool transformUpdated);
method.
Is that right? Am I missing something? It seems a backward step to me? When I just want to override the draw method of a layer and don’t require access to the renderer, transform or transformUpdated arguments, it’d be nice to still be able to do so …
Yep. You are right.
By doing so is to improve performance and remove the dependence of math lib.
@zhangxm
need ur help
i’ve got a problem cocos2dx 2.2.3.
CCLayerColor::initWithColor(ccc4(255,255,255,255));
then i override draw() in helloworld,
and the background turned into black ,
can’t solve.
plz help,thx
Maxxx
March 25, 2014, 2:55am
#4
@a83988029 My guess is you are not calling the base draw() function from your overrideen function…
@Maxxx let me try as you said
i didnt understand how you override the draw metod in rc0 can you explain it to me? why all those parameters in the method?
Maxxx
March 29, 2014, 4:43am
#8
@agucabral21
The way I have done it is to override the new virtual draw function with all the parameters, then I have
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(WorldLayer::drawSubterrains, this);
renderer->addCommand(&_customCommand);
which adds my drawSubterrains method to the rendering pipeline.
dalste
March 29, 2014, 2:22pm
#10
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
protected:
CustomCommand _customCommand;
virtual void draw(Renderer *renderer, const kmMat4& transform, bool transformUpdated)
{
_customCommand.init(_globalZOrder+10);
_customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
}
virtual void onDraw(const kmMat4 &transform, bool transformUpdated)
{
float selfHeight = this->getContentSize().height;
float selfWidth = this->getContentSize().width;
cocos2d::Point vertices[4] = {cocos2d::Point(10.f, 10.f), cocos2d::Point(10.f, selfHeight-10), cocos2d::Point(selfWidth-10, selfHeight-10), cocos2d::Point(selfWidth-10, 10.f)};
cocos2d::DrawPrimitives::drawSolidPoly(vertices, 4, Color4F(0,1,0,1));
}
private:
CCTMXTiledMap *_tileMap;
CCTMXLayer *_background;
};
#endif // __HELLOWORLD_SCENE_H__
dalste
March 29, 2014, 2:24pm
#11
note the initialization of custom command with a z-order higher than that of the the helloworld layer