【问题标题】:Cocos2d-x boundingBox issueCocos2d-x boundingBox 问题
【发布时间】:2013-08-24 17:06:51
【问题描述】:

我正在为 Android 开发一个 cocos2d-x 游戏,但遇到了问题。

    _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 比看起来要大。当我单击边界框附近的某个位置时,它会注册,就好像我实际上在其中单击一样。

请任何人都可以帮助我吗? :)

【问题讨论】:

    标签: android cocos2d-iphone cocos2d-x cocos2d-android


    【解决方案1】:

    边界框的矩形与图像的内容大小相同。它包含图像的 alpha 区域。

    您可以使用'TexturePacker'之类的工具来修剪图像,然后在加载后使用getTextureRect得到最小的矩形。

    【讨论】:

      【解决方案2】:

      试试这个:

       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
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多