cocos2d-x Touch触屏事件

单点触控

1. OnEnter或init中添加开启触屏接收属性:

setTouchEnabled(true);

2. 重载

virtual void registerWithTouchDispatcher(void);

并添加:

CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0, true)

3. 重载需要的响应函数

ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  

ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

 

下面看一个例子:

新建HelloWorld, AppDelegate.cpp的applicationDidFinishLaunching注释掉原有HelloWorld的,添加CCScene *pScene = GameScene::scene();

为工程添加新类GameScene.定义如下:

#pragma once
#include "cocos2d.h"

using namespace cocos2d;

class GameScene :
	public CCLayerColor
{
public:
	static cocos2d::CCScene* scene();
public:
	virtual bool init();

 	virtual bool ccTouchBegan(CCTouch *pTouches, CCEvent *pEvent);
 	virtual void ccTouchMoved(CCTouch *pTouches, CCEvent *pEvent);
	virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
	virtual void registerWithTouchDispatcher();

	void menuCloseCallback(CCObject* pSender);
	CREATE_FUNC(GameScene);

	void updateSize(CCPoint p);
};

 

#include "GameScene.h"
#include "HelloWorldScene.h"

using namespace cocos2d;

CCScene* GameScene::scene()
{
	CCScene* scene = CCScene::create();
	GameScene *gameScene = GameScene::create();
	scene->addChild(gameScene);
	return scene;
}

bool GameScene::init()
{
	CCLayerColor::init();

	CCSize size = CCDirector::sharedDirector()->getWinSize();

	setTouchEnabled(true);

	CCLayerColor *colorRect = CCLayerColor::create(ccc4(255, 255, 0, 255), 200.0, 200.0);

	colorRect->setPosition(ccp(size.width/2, size.height/2));
	colorRect->ignoreAnchorPointForPosition(false);
	addChild(colorRect, 0, 1);

	CCActionInterval *colorAction = CCRepeatForever::create((CCActionInterval *)CCSequence::create(
		CCTintTo::create(0.2f, 255, 0, 0),
		CCTintTo::create(0.2f, 0, 255, 0),
		CCTintTo::create(0.2f, 0, 0, 255),
		CCTintTo::create(0.2f, 0, 255, 255),
		CCTintTo::create(0.2f, 255, 255, 0),
		CCTintTo::create(0.2f, 255, 0, 255),
		CCTintTo::create(0.2f, 255, 255, 255),
		NULL));

	colorRect->runAction(colorAction);

	return true;
}

bool GameScene::ccTouchBegan(CCTouch *pTouches, CCEvent *pEvent)
{
	CCPoint point = pTouches->getLocation();
	updateSize(point);
	return true;
}


void GameScene::ccTouchMoved(CCTouch *pTouches, CCEvent *pEvent)
{
	CCPoint point = pTouches->getLocation();
	updateSize(point);
}

void GameScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint point = pTouch->getLocation();
	updateSize(point);
}

void GameScene::updateSize(CCPoint p)
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSize newSize = CCSizeMake( fabs(p.x-size.width/2)*2, fabs(p.y-size.height/2)*2 );
	CCLayerColor *layer = (CCLayerColor*)getChildByTag(1);
	layer->setContentSize(newSize);
}

void GameScene::registerWithTouchDispatcher()
{
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority + 1, true);
}

运行点击屏幕,拖动屏幕可看到效果。

 

CCDirector的addTargetedDelegate的作用是在Dispatcher的列表中注册一个触屏事件的委托,用来监听用户的触屏事件。

参数分别为:触屏世家你委托CCTouchDelegate目标,优先级(值越高越早被响应),是否拦截触屏事件(true表示拦截触屏事件,即响应响应本次事件委托后,不再继续分发触屏事件到Dispatcher列表的中其他委托)。


多点触控

多点触控与单点触控类似,只不过是在触控的一串输入中,遍历这一串经行处理,在TestCpp的例子中有一个专门的例子 MutiTouchTest

所用到的函数有:

ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)

void MutiTouchTestLayer::registerWithTouchDispatcher(void)
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
}


你可能感兴趣的:(C++,cocos2d-x)