在.h中先申明
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch,
cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch,
cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch,
cocos2d::CCEvent *pEvent);
.m文件
在init方法实现touch的代理方法
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);//设置单点触摸
//------------------------------------------------------------------------------------
void HelloWorld::ccTouchEnded(CCTouch*pTouch, CCEvent *pEvent){
CCPoint touchPoint = pTouch->locationInView();
printf("%f,%f",touchPoint.x,touchPoint.y);
pSprite->setPosition(touchPoint);
}
void HelloWorld::ccTouchMoved(CCTouch*pTouch, CCEvent *pEvent){
printf("hhhh");
CCPoint touchPoint = pTouch->locationInView();
printf("%f,%f",touchPoint.x,touchPoint.y);
}