2013-08-24 45 views
0

我正在開發一款適用於Android的cocos2d-x遊戲,並遇到問題。Cocos2d-x boundingBox問題

_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 
    } 

我打開CC_SPRITE_DEBUG_DRAW和CC_SPRITEBATCHNODE_DEBUG_DRAW以及存在的問題是,boundingBox的是大於它似乎是。當我點擊邊界框附近的某處時,它會註冊,就好像我真的在裏面點擊了一樣。

請任何人都可以幫我嗎? :)

回答

3

邊界框的矩形與圖像的內容大小相同。它包含圖像的alpha區域。

您可以使用'TexturePacker'等工具修剪圖像,然後在加載後使用getTextureRect來獲得最小的矩形。

0

試試這個:

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 
}