We can use DrawNode to draw some useful information for debugging, or something more sophisticated as a health bar above char’s head or just something we need to hit by a click that is my case.
My issue consist to retrieve the BoundingBox from a rectangle made by a DrawNode. All information I get is 0, nothing about position (x, y) and its size (width, height). I need it to create a touch event to check if user is touching inside the rectangle’s area or not.
The process to create the the rects:
DrawNode* Player::createRect(cocos2d::Vec2 p1, cocos2d::Vec2 p2, cocos2d::Vec2 p3, cocos2d::Vec2 p4) {
float alpha = 0.8;
auto rect = DrawNode::create();
Vec2 vertices[] = {p1, p2, p3, p4};
rect->setAnchorPoint(Vec2(0, 0));
rect->drawPolygon(vertices, 4, Color4F(1.0, 1.0, 1.0, alpha), 1, Color4F(0.2, 0.2, 0.2, alpha));
_sprite->addChild(rect, -1);
return rect;
}
Above function is working as well, I create the rects and add them as a child to a player’s sprite. The snippet below shows up how I’m trying to capture the bounding of these rects by user’s touch.
…
if (_player->rectUp.box->getBoundingBox().containsPoint(touchLocation))
direction = Direction::UP;
else if (_player->rectDown.box->getBoundingBox().containsPoint(touchLocation))
direction = Direction::DOWN;
else if (_player->rectLeft.box->getBoundingBox().containsPoint(touchLocation))
direction = Direction::LEFT;
else if (_player->rectRight.box->getBoundingBox().containsPoint(touchLocation))
direction = Direction::RIGHT;
…
How can I get the exactly dimension of these rects even bounding box returning nothing for me? Need I to replace it to a baked sprite to make cocos2d-x understand the position and it’s size? I still want to use DrawNode to achieve this if possible.