I’m using cocos2d-x 3.1, and I want to draw a boundingbox
void GameHelper::DrawBoundingBox(Node* target, Color4B color)
{
glLineWidth(10);
Rect r = target->getParent()->boundingBox();
Point o = r.origin;
Point w = ccp(o.x + r.size.width, o.y + r.size.height);
DrawPrimitives::setDrawColor4B(color.r,color.g,color.b,color.a);
DrawPrimitives::setPointSize(1);
DrawPrimitives::drawLine(o, w);
glEnable(GL_LINE_SMOOTH);
}
If you don’t want to use an extra Node to draw a rect.
You could wrap your DrawPrimitives call into a CustomCommand object and send it to the renderer.
Some thing like this:
Every single draw primitives call should goes into the function of DrawPrimitives::onDraw, in your case , you could declare a function named GameHelper::onDraw.
For more information, please take a look at the DrawPrimitiveTest.
I did a try on this solution. It works but it need some more effort on resizing. That is, you need to clear the Node, and call drawPolygon again as below. I set stroke width as Zero as I think it will make the region larger than expected.
If you use LayerColor, we simply need to resize the region. But of course there are limitations on using LayerColor. e.g, cannot empty fill and set stroke only.
@owen
Besides the test you mentioned, are there any official document about how to customize drawing of a Node?
I tried to derive a class from Node, then override its draw(…) (not the final one) function. But it won’t work, nothing was drawn for that derived Node. I spent some time trying to search some information. It seems the draw() function will not be called anymore? Cocos2d 3.x is using visit and send commands to the renderer now, right? Please point me some documents or guides about this topic. Thanks a lot!
I got a question. How to modify the line width if using DrawNode to draw it.
what’s the unit of 4th parameter of drawPolygon function? it seems not to be in pixel.
If I want to draw a polygon with 10 pixels line width, how to do that?