I want to do the simplest thing in the world via cocos2d-x and and I have problems. I need a white layer on that a red rectangle. First I have found out the in order to color a layer I have to derive from cocos2d::LayerColor and LayerColor::initWithColor(Color4B(255, 255, 255, 255)) then I learnt that in order to draw a rectangle I should override draw method like this:
void HelloWorld::draw()
{
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}
This things work separatlely, but they don’t work together. I guess when I override draw, then all drawing reduces to these two function calls, hence the background of my layer becomes black. Where is the solution of this simplest thing?
EDIT:
I have tried to call LayerColor::draw in the overridden one like this:
void HelloWorld::draw()
{
LayerColor::draw();
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}
Didn’t help. Tried to derive from sprite and addChild to the layer like this:
class BoardView : public Sprite
{
public:
BoardView() : Sprite()
{
}
virtual void draw() override
{
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}
};
But this didn’t work either! How can I do this simplest thing? Did I miss something?