本文共 3673 字,大约阅读时间需要 12 分钟。
说明:这篇文章引用过来,主要提供给对这部分内容不太熟悉的学员参考。
FROM:http://blog.csdn.net/h1023417614/article/details/20390153
前提是继承CCLayer
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.定义如下:
[cpp]
#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);
};
[cpp]
#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);}