bool ccTouchBegan(是cocos2d:: CCTouch * pTouch,是cocos2d:: CCEvent *增加); 空白ccTouchEnded(是cocos2d:: CCTouch * pTouch,是cocos2d:: CCEvent *增加); 空白ccTouchMoved(是cocos2d:: CCTouch * pTouch,是cocos2d:: CCEvent *增加),
。cpp文件实现如下:
bool HelloWorld: init () {////////////////////////////////1。超级init第一 如果(! CCLayer: init ()) { 返回错误; }//在实现单点触摸之前,需要为我们的触摸事件进行注册://这里的三个参数,第一个:是哪个对象进行触摸注册,第二个:触摸优先级第三个是否吞噬掉本次触摸事件 CCDirector: sharedDirector ()→getTouchDispatcher ()→addTargetedDelegate(0,这真实);//所谓触摸事件的优先级,就是如果我们在一个场景中注册了多个触摸事件,谁会先接受到这次触摸事件,优先级数值越小,优先级越高,也就会优先得到这次触摸事件,它可以选择是否处理这次触摸,以及是否将这次触摸传递下去。值得一提的是,CCMenu的优先级是-128。//所谓吞噬事件,就是说如果我们选择,并且处理这个事件,那么触摸事件就不会发送给其他代理。 返回true; }
//开始触摸的回调方法 bool HelloWorld:: ccTouchBegan(是cocos2d:: CCTouch * pTouch,是cocos2d:: CCEvent *增加) {//获取openGL坐标 CCPoint openglPoint=pTouch→getLocation (); CCLog (“openGL=% f % f”, openglPoint.x, openglPoint.y);//获取屏幕坐标,原点在左上角 CCPoint screenPoint=pTouch→getLocationInView (); CCLog(“屏幕坐标=% f % f”, screenPoint.x, screenPoint.y); 返回true; }
//触摸移动时的回调方法 空白HelloWorld:: ccTouchMoved(是cocos2d:: CCTouch * pTouch,是cocos2d:: CCEvent *增加) {//获得前一个点的坐标 CCPoint曾经繁荣=pTouch→getPreviousLocation ();//获得当前点的坐标是按屏幕的坐标获取的就是说以左上角为原点。 CCPoint端点=pTouch→getLocation (); CCLog (“startPointX=% f, startPointY=% f”, startPoint.x, startPoint.y); CCLog (“endPointX=% f, endPointY=% f”, endPoint.x, endPoint.y); }
//触摸结束时的回调方法 空白HelloWorld:: ccTouchEnded(是cocos2d:: CCTouch * pTouch,是cocos2d:: CCEvent *增加) { }
代码下载http://pan.baidu.com/share/link?shareid=2820307714&英国=3189484501