在.h中声明:
virtual void registerWithTouchDispatcher(void);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
并在.cpp的init()中设置:
this->setTouchEnabled(true);
下面就是Touch函数的实现:
void DragScene::registerWithTouchDispatcher(void){
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
bool DragScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
mBullet->setPosition(ccp(pTouch->getLocation().x , pTouch->getLocation().y));
return true;
}
void DragScene::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
mBullet->setPosition(ccp(pTouch->getLocation().x , pTouch->getLocation().y));
}
void DragScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
}
void DragScene::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
}
在触摸的时候让精灵跟随触摸移动,也就是在ccTouchMoved中更新精灵的位置。
效果图:
转载请注明出处:http://blog.csdn.net/Vestigge