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
__TOUCH_H__

#include
"cocos2d.h"

USING_NS_CC;

class
Touch :public
CCLayerColor

{

public:

static
CCScene *
scene();

CREATE_FUNC(Touch);

bool
init();

enum
MyEnum

{

BN747

};

bool
ccTouchBegan(CCTouch
*pTouch,
CCEvent *pEvent);

void
ccTouchMoved(CCTouch
*pTouch,
CCEvent *pEvent);

void
ccTouchEnded(CCTouch
*pTouch,
CCEvent *pEvent);

void
ccTouchCancelled(CCTouch
*pTouch,
CCEvent *pEvent);

};

#endif

Touch.cpp

#include
"Touch.h"

#include
"AppMacros.h"

CCScene *Touch::scene()

{

CCScene *
scene =
CCScene::create();

Touch *
layer =
Touch::create();

scene->addChild(layer);

return
scene;

}

bool
Touch::init()

{

CCLayerColor::initWithColor(ccc4(255,
255, 255, 255));

setTouchEnabled(true);

setTouchMode(kCCTouchesOneByOne);

CCSprite *
plane =
CCSprite::create("hero1.png");

plane->setPosition(ccp(100,
100));

addChild(plane);

plane->setTag(BN747);

return
true;

}

bool
Touch::ccTouchBegan(CCTouch
*pTouch,
CCEvent *pEvent)

{

CCSprite *
plane = (CCSprite
*)getChildByTag(BN747);

CCPoint
pt =
pTouch->getLocation();

if (plane->boundingBox().containsPoint(pt))

{

CCLOG("touched");

return
true;

}

//决定消息是否往下传递

return
false;

}

void
Touch::ccTouchMoved(CCTouch
*pTouch,
CCEvent *pEvent)

{

CCSprite *
plane = (CCSprite
*)getChildByTag(BN747);

CCPoint
pt =
plane->getPosition()
+ pTouch->getDelta();

if (pt.x
> plane->getContentSize().width
/ 2

&& pt.x
< winSize.width
- plane->getContentSize().width
/ 2

&& pt.y
> plane->getContentSize().height
/ 2

&& pt.y
< winSize.height
- plane->getContentSize().height
/ 2)

{

plane->setPosition(pt);

}

CCLog("ccTouchMoved
x = %g y = %g",
pt.x,
pt.y);

}

void
Touch::ccTouchEnded(CCTouch
*pTouch,
CCEvent *pEvent)

{

CCLog("ccTouchEnded");

}

void
Touch::ccTouchCancelled(CCTouch
*pTouch,
CCEvent *pEvent)

{

CCLog("ccTouchCancelled");

}

执行结果:

5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬-LMLPHP

05-12 03:58