0

I am developing a game in cocos2d-x for Android and I have run into a problem.

    _hammer = CCSprite::createWithSpriteFrameName("Hammer.png");
    _hammer->setPosition(ccp(_screenSize.width * 0.5f - 4*((_hammer->getContentSize().width) * 0.15f), _screenSize.height * 0.055f ));
    _hammer->setVisible(true);
    _hammer->setScale((_screenSize.width / _hammer->getContentSize().width) * 0.15f);
    _hammer->setScale((_screenSize.height / _hammer->getContentSize().height) * 0.15f);
    _hammerSelected = true;


 {
    CCTouch *touch = (CCTouch *) pTouches->anyObject();
    CCPoint location = touch->locationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);
    if ((CCRect::CCRectContainsPoint(_hammer->boundingBox(), location))) {
    //do something
    }

I turned on CC_SPRITE_DEBUG_DRAW and CC_SPRITEBATCHNODE_DEBUG_DRAW and the problems is that the boundingBox is bigger than it appears to be. When I click somewhere near the boundingBox it registers as if I actually clicked inside it.

Please can anyone help me out ? :)

2 Answers2

3

The rect of bounding box is the same as the content size of the image. It contains the alpha area of the image.

You can use tools like 'TexturePacker' to trim the image, then after loaded, use getTextureRect to get the smallest rect.

WilliamX
  • 66
  • 3
0

Try this:

 CCRect rect= _hammer->boundingBox();

 //multiply the scale value.
 rect.size.height*= _hammer->getScaleX();
 rect.size.width*= _hammer->getScaleY();

 if ((CCRect::CCRectContainsPoint(rect, location))) {
 //do something
 }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
KARTHIK RA
  • 469
  • 2
  • 8