一、精灵图片实现触屏检测方法:
1.创建精灵
testSpr = CCSprite::create("Icon.png");
testSpr->setPosition(ccp(600, 400));
this->addChild(testSpr);
testSpr = CCSprite::create("Icon.png");
testSpr->setPosition(ccp(600, 400));
this->addChild(testSpr);
2.注册触屏事件
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,
-127, false);
setTouchEnabled(true);
setTouchEnabled(true);
3.实现检测
bool MainUI::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{CCPoint location = pTouch->getLocation();
CCRect bBox = testSpr->boundingBox(); //得到精灵的碰撞盒
if (bBox.containsPoint(location)) {
CCLOG("is touched! la la la ~~~");
}
return false;
}