[Cocos2D-X ]初窥门径(4)Touch 事件:移动精灵
在.h中声明:
[cpp]
TouchDispatcher(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()中设置:
[cpp]
this->setTouchEnabled(true);
下面就是Touch函数的实现:
[cpp]
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中更新精灵的位置。
效果图:
补充:移动开发 , 其他 ,