Cocos2d x 如何在Cocos2Dx中处理触摸

Cocos2d x 如何在Cocos2Dx中处理触摸,cocos2d-x,Cocos2d X,我的问题是,当触摸屏幕上的任何位置时,精灵就不可见了。但我想这样做,精灵应该是无形的,只有当我点击精灵 bool CharacterSelection::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent { CCTouch* touch; CCPoint tap = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView()); Goa

我的问题是,当触摸屏幕上的任何位置时,精灵就不可见了。但我想这样做,精灵应该是无形的,只有当我点击精灵

bool CharacterSelection::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent
{

    CCTouch* touch;
    CCPoint tap =  CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
    GoatSprite* goat;
    goat = (GoatSprite*) goatSpriteObject;
    if(touch){
        tap = touch->getLocation();    
    }
    if(goat->boundingBox().containsPoint(tap)){
        goat->setVisible(false);
    }
    return true;
}

如果代码中有错误,很抱歉。

我觉得你的代码很奇怪

  • CCEvent*pEvent

  • 您已经有一个touch作为
    pTouch
    输入,无需创建新的
    CCTouch*touch

  • 您正在使用
    if(touch)
    而您什么也没做,这将是
    pTouch

  • 您甚至不需要
    if(touch)
    条件

  • 尝试简化为:

    cocos2d::CCPoint p = pTouch->getLocation();
    cocos2d::CCRect rect = goat->getBoundingBox();
    
    if(rect.containsPoint(p))
    {
         // you touched it              
    }