Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Last active August 29, 2015 14:10
Show Gist options
  • Save kankikuchi/a2633039479c5979fb59 to your computer and use it in GitHub Desktop.
Save kankikuchi/a2633039479c5979fb59 to your computer and use it in GitHub Desktop.
CCMenuItemSpriteのタッチ可能範囲(当たり判定)だけ広げる【cocos2dx】
// タッチ判定を広くしたCCMenuItemSpriteを作成
CCMenuItemSprite* Function::CreateCCMenuItemSpriteWideContentSize(float minContentSize, const char* imageName, CCObject* target, SEL_MenuHandler selector){
CCSprite* normal = CCSprite::createWithSpriteFrameName(imageName);
CCSprite* selected = CCSprite::createWithSpriteFrameName(imageName);
selected->setColor(ccGRAY);
//ボタンの当たり判定の倍率を計算
float contentRateX = MAX(1.0f, minContentSize / normal->getContentSize().width);
float contentRateY = MAX(1.0f, minContentSize / normal->getContentSize().height);
//ボタン画像の位置を動かして当たり判定を上下左右に広げるように
normal->setPosition(ccp(normal->getPositionX() + normal->getContentSize().width * (contentRateX - 1.0f) * 0.5f,
normal->getPositionY() + normal->getContentSize().height * (contentRateY - 1.0f) * 0.5f));
selected->setPosition(normal->getPosition());
//ボタン作成
CCMenuItemSprite* itemSprite = CCMenuItemSprite::create(normal, selected, target, selector);
//ボタンの当たり判定を広げる
itemSprite->setContentSize(ccp(
normal->getContentSize().width * contentRateX,
normal->getContentSize().height * contentRateY
));
return itemSprite;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment