1
触摸
Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能。
2
单点触摸
打开触摸开关和触摸方式
setTouchEnabled(true);
setTouchMode(kCCTouchesOneByOne);
Cocos2dx
对触摸分三布来处理。分是是点触。移动。离开。
或是中间被打断。
其功能皆有对应的virtual
函数进行override
的。
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); |
获取点击的坐标点
CCPoint pt = pTouch->getLocation(); CCLog(“x=%f y=%f”,pt.x,pt.y); |
理解getDelta();
ccTouchBegan start x = 67.309761 y = 180.989975 ccTouchMoved delta x = 1.013191, y = 0.000000 ccTouchMoved delta x = 5.977837, y = 0.000000 ccTouchMoved delta x = 3.985229, y = 0.000000 ccTouchMoved delta x = 1.013184, y = 0.000000 ccTouchMoved delta x = 1.992615, y = 0.000000 ccTouchMoved ................................... ................................... delta x = 1.013184, y = 0.000000 ccTouchMoved delta x = 0.979431, y = 0.000000 ccTouchMoved delta x = 1.992615, y = 0.000000 ccTouchMoved delta x = 3.005798, y = 0.000000 ccTouchMoved delta x = 3.005798, y = 0.000000 ccTouchMoved delta x = 2.972015, y = 0.000000 ccTouchMoved delta x = 1.013214, y = 0.000000 ccTouchMoved delta x = 3.005798, y = 0.000000 ccTouchMoved delta x = 0.979431, y = 0.000000 ccTouchEnded |
3
多点触摸
打开触摸开关,系统默觉得单点触摸。
setTouchEnabled(true); |
cocos2dx对触摸分三部来处理。各自是点触,移动。离开。或是中间被打断。
其功能皆有对应的virtual函数进行override的。
virtual bool ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); |
获取点击的坐标点。
4
案例(打飞机)
Touch.h |
#ifndef __TOUCH_H__ #define #include USING_NS_CC; class { public: static CREATE_FUNC(Touch); bool enum { BN747 }; bool void void void }; #endif |
Touch.cpp |
#include "Touch.h" #include CCScene *Touch::scene() { CCScene * Touch * scene->addChild(layer); return } bool { CCLayerColor::initWithColor(ccc4(255, setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); CCSprite * plane->setPosition(ccp(100, addChild(plane); plane->setTag(BN747); return } bool { CCSprite * CCPoint if (plane->boundingBox().containsPoint(pt)) { CCLOG("touched"); return } //决定消息是否往下传递 return } void { CCSprite * CCPoint if (pt.x && pt.x && pt.y && pt.y { plane->setPosition(pt); } CCLog("ccTouchMoved } void { CCLog("ccTouchEnded"); } void { CCLog("ccTouchCancelled"); } |
执行结果: |