触摸控制
单点触控
TouchTestLayer.h
#ifndef _TOUCH_TEST_LAYER_H_
#define _TOUCH_TEST_LAYER_H_
#include "cocos2d.h"
USING_NS_CC;
class TouchTestLayer : public Layer
{
public:
static cocos2d::Scene* createScene();
CREATE_FUNC(TouchTestLayer);
virtual bool init();
virtual bool onTouchBegan(Touch * touch, Event *unused_event);
virtual void onTouchMoved(Touch *touch, Event *unused_event);
virtual void onTouchEnded(Touch *touch, Event *unused_event);
};
#endif
TouchTestLayer.cpp
可以使用拉姆达表达式来实现事件的相应,但在处理的事件较多时,应单独写成方法
#include "TouchTestLayer.h"
Scene* TouchTestLayer::createScene()
{
auto scene = Scene::create();
auto layer = TouchTestLayer::create();
scene->addChild(layer);
return scene;
}
bool TouchTestLayer::init()
{
Layer::init();
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true); //只响应它自己,吞噬其它点击事件
//listener->onTouchBegan = [this](Touch * touch, Event * event)
//{
// log("touch began");
// Vec2 location = touch->getLocation(); //得到的是OpenGL坐标
// location = this->convertToNodeSpace(location);//坐标系转换,转换为当前坐标系中的一个坐标
//
// auto sprite = Sprite::create("CloseNormal.png");
// this->addChild(sprite);
// sprite->setPosition(location);
// return true;
// //return false 可以使得后面moved ended无法执行,新手引导,可以用到,固定触摸某个范围
//};
拉姆达表达式 【】 可填写 空 = 可调用变量 this/&(取地址) 可改变外部变量
//listener->onTouchMoved = [this](Touch * touch, Event * event)
//{
// log("touch moved");
//};
//listener->onTouchEnded = [this](Touch * touch, Event * event)
//{
// log("touch ended");
// Vec2 location = touch->getLocation(); //得到的是OpenGL坐标
// location = this->convertToNodeSpace(location);//坐标系转换,转换为当前坐标系中的一个坐标
// auto sprite = Sprite::create("CloseSelected.png");
// this->addChild(sprite);
// sprite->setPosition(location);
//};
listener->onTouchBegan = CC_CALLBACK_2(TouchTestLayer::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(TouchTestLayer::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(TouchTestLayer::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
//事件分发器。
return true;
}
bool TouchTestLayer::onTouchBegan(Touch * touch, Event *unused_event)
{
log("touch began");
Vec2 location = touch->getLocation(); //得到的是OpenGL坐标
location = this->convertToNodeSpace(location);//坐标系转换,转换为当前坐标系中的一个坐标
auto sprite = Sprite::create("CloseNormal.png");
this->addChild(sprite);
sprite->setPosition(location);
return true;
return true;
};
void TouchTestLayer::onTouchMoved(Touch * touch, Event *unused_event)
{
log("touch moved");
};
void TouchTestLayer::onTouchEnded(Touch * touch, Event *unused_event)
{
log("touch ended");
Vec2 location = touch->getLocation(); //得到的是OpenGL坐标
location = this->convertToNodeSpace(location);//坐标系转换,转换为当前坐标系中的一个坐标
auto sprite = Sprite::create("CloseSelected.png");
this->addChild(sprite);
sprite->setPosition(location);
}
注: 只有onTouchBegan 是有返回值得,若返回值为false,则不会继续执行onTouchMoved 和 onTouchEnded 方法。
多点触控
MutiTouchTest.h
#ifndef _MUTI_TOUCH_TEST_H_
#define _MUTI_TOUCH_TEST_H_
#include "cocos2d.h"
USING_NS_CC;
class MutiTouchTest : public Layer
{
public:
static cocos2d::Scene* createScene();
CREATE_FUNC(MutiTouchTest);
virtual bool init();
virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
};
#endif
MutiTouchTest.cpp
#include "MutiTouchTest.h"
Scene* MutiTouchTest::createScene()
{
auto scene = Scene::create();
auto layer = MutiTouchTest::create();
scene->addChild(layer);
return scene;
}
bool MutiTouchTest::init()
{
Layer::init();
auto lisener = EventListenerTouchAllAtOnce::create();
lisener->onTouchesBegan = CC_CALLBACK_2(MutiTouchTest::onTouchesBegan,this);
lisener->onTouchesMoved = CC_CALLBACK_2(MutiTouchTest::onTouchesMoved, this);
lisener->onTouchesEnded = CC_CALLBACK_2(MutiTouchTest::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(lisener,this); //事件分发器
return true;
}
void MutiTouchTest::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)
{
log("MutiTouchTest::onTouchesBegan");
for (auto touch:touches)
{
Vec2 location = touch->getLocation();
location = this->convertToNodeSpace(location);
}
};
void MutiTouchTest::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event)
{
log("MutiTouchTest::onTouchesMoved");
};
void MutiTouchTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)
{
log("MutiTouchTest::onTouchesEnded");
}
注:和单点触控类似, 需注意的是调用的是 on Touches*** 方法,若还写为 onTouch*** 则会出错。另外调用方法时候传入的参数不同。
多点触控无法再电脑上进行测试,还需要移动设备。