LoginSignup
6
5

More than 5 years have passed since last update.

【cocos2d-x】どのCCSpriteがタッチされたか判断する

Last updated at Posted at 2014-05-21

流れ

1,CCSpriteにTagをセット
2,ccTouchEnded関数にてタッチイベントを適切に処理する

CCSpriteにTagをセット

int hoge = HOGE_NUMBER;

CCSprite* pCard = CCSprite::create("画像名");
pCard -> setTag(hoge);

ccTouchEnded関数にてタッチイベントを適切に処理する

①ccTouchBegan、ccTouchEnded関数ともにヘッダーで定義
②ccTouchEndedに処理を追加

bool クラス名::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent)
{
    return true;
}

void クラス名::ccTouchEnded(CCTouch* pTouch, CCEvent *pEvent)
{
    int hogeTagNumber = HOGE_TAG_NUMBER

    //タッチポイントを取得(convertToGLにてOpenGLの座標に変換)
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCPoint touchPoint = pDirector -> convertToGL(pTouch -> getLocationInView());

    //タグのインスタンスを取得
    CCNode* pHoge = this -> getChildByTag(hogeTagNumber);

    if (!pHoge) {
    //pHogeインスタンスが取得できなかった場合は処理を抜ける
        return;
    }

    //pHogeのサイズを取得する
    CCRect hogeRect = pHoge -> boundingBox();

    if (hogeRect.containsPoint(touchPoint)) {
        //タッチポイントがhogeRectの範囲内    
       //以下にそれぞれの処理を追加する

    }
}

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5