TouchableSpriteTest

auto winSize = Director::getInstance()->getWinSize();

    Point origin = Director::getInstance()->getVisibleOrigin();

    auto containerForSprite1 = Node::create();
auto sprite1 = Sprite::create("CyanSquare.png");
sprite1->setPosition(origin + Point(winSize.width / 2, winSize.height / 2) + Point(-80, 80));
containerForSprite1->addChild(sprite1);
addChild(containerForSprite1,10); auto sprite2 = Sprite::create("MagentaSquare.png");
sprite2->setPosition(origin + Point(winSize.width / 2, winSize.height / 2));
addChild(sprite2,20); auto sprite3 = Sprite::create("YellowSquare.png");
sprite3->setPosition(Point(0, 0));
sprite2->addChild(sprite3, 1); auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true); listener1->onTouchBegan = [](Touch* touch,Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode)) {
log("sprite began... x = %f, y = %f",locationInNode.x , locationInNode.y);
target->setOpacity(180);
return true;
}
return false;
}; listener1->onTouchMoved = [](Touch* touch,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta()); }; listener1->onTouchEnded = [=](Touch* touch,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchedEnded"); target->setOpacity(255);
if (target == sprite2) {
containerForSprite1->setLocalZOrder(100); }
else if (target == sprite1)
{
containerForSprite1->setLocalZOrder(0);
}
}; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3); auto removeAllTouchItem = MenuItemFont::create("Remove All Touch Listeners", [this](Ref* sender){ auto senderItem = static_cast<MenuItemFont*>(sender);
senderItem->setString("Only Next item could be clicked"); _eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE); auto nextItem = MenuItemFont::create("Next", [=](Ref* sender){ }); }); removeAllTouchItem->setFontSizeObj(16);
removeAllTouchItem->setPosition(Point(100, 50)); auto menu = Menu::create(removeAllTouchItem, NULL);
menu->setPosition(Point(0, 0));
menu->setAnchorPoint(Point(0, 0));
addChild(menu);

效果:

CustomEventTest

Point origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize(); MenuItemFont::setFontSize(20); auto statusLabel = Label::createWithSystemFont("No custom event 1 received!", "", 20);
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
addChild(statusLabel); _listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){
std::string str("Custom event 1 received, ");
char* buf = static_cast<char*>(event->getUserData());
str += buf;
str += " times";
statusLabel->setString(str.c_str());
}); _eventDispatcher->addEventListenerWithFixedPriority(_listener, 1); auto sendItem = MenuItemFont::create("Send Custom Event 1", [=](Ref* sender){
static int count = 0;
++count; char* buf = new char[10];
sprintf(buf, "%d",count);
EventCustom event("game_custom_event1");
event.setUserData(buf);
_eventDispatcher->dispatchEvent(&event);
CC_SAFE_DELETE_ARRAY(buf); });
sendItem->setPosition(origin + Point(size.width / 2, size.height / 2)); auto menu = Menu::create(sendItem, NULL);
menu->setPosition(Point(0, 0));
menu->setAnchorPoint(Point(0, 0));
addChild(menu , -1);

RemoveAndRetainNodeTest

    Point origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize(); _sprite = Sprite::create("CyanSquare.png");
_sprite->setPosition(origin+Point(size.width/2, size.height/2));
addChild(_sprite, 10); // Make sprite1 touchable
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = [](Touch* touch ,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode)) {
log("sprite began... x = %f, y = %f",locationInNode.x,locationInNode.y);
target->setOpacity(180);
return true;
}
return false; }; listener1->onTouchMoved = [](Touch* touch ,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
}; listener1->onTouchEnded = [](Touch* touch,Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded..");
target->setOpacity(25);
}; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _sprite); this->runAction(Sequence::create(DelayTime::create(5.0f),CallFunc::create([this](){
_spriteSaved = true;
_sprite->retain();
_sprite->removeFromParent();
}),DelayTime::create(5),CallFunc::create([this](){
_spriteSaved = false;
this->addChild(_sprite);
_sprite->release();
})
, NULL));

RemoveListenerAfterAddingTest

auto item1 = MenuItemFont::create("Click Me 1", [this](Ref* sender){
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch,Event* event)->bool{
CCASSERT(false, "Should not come here!");
return true;
}; _eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
_eventDispatcher->removeEventListener(listener); }); item1->setPosition(Point(0, 80)); auto addNextButton = [this](){ auto next = MenuItemFont::create("Please Click Me To Reset!", [this](Ref* sender){ }); next->setPosition(Point(100, 100)); auto menu = Menu::create(next, NULL);
menu->setPosition(Point(300, 100));
menu->setAnchorPoint(Point::ZERO);
this->addChild(menu); }; auto item2 = MenuItemFont::create("Click Me 2", [=](Ref* sender){
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
CCASSERT(false, "Should not come here!");
return true;
}; _eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
_eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE); addNextButton();
}); item2->setPosition( Point(0, 40)); auto item3 = MenuItemFont::create("Click Me 3", [=](Ref* sender){
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
CCASSERT(false, "Should not come here!");
return true;
}; _eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
_eventDispatcher->removeAllEventListeners(); addNextButton();
});
item3->setPosition(Point(90, 90)); auto menu = Menu::create(item1,item2,item3, NULL);
menu->setPosition(Point(160, 160));
addChild(menu);

DirectorEventTest

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} Size s = Director::getInstance()->getWinSize(); TTFConfig ttfConfig("arial.ttf", 20); _label1 = Label::createWithTTF(ttfConfig, "Update: 0");
_label1->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
_label1->setPosition(30,s.height/2 + 60);
this->addChild(_label1); _label2 = Label::createWithTTF(ttfConfig, "Visit: 0");
_label2->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
_label2->setPosition(30,s.height/2 + 20);
this->addChild(_label2); _label3 = Label::createWithTTF(ttfConfig, "Draw: 0");
_label3->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
_label3->setPosition(30,30);
_label3->setPosition(30,s.height/2 - 20);
_label3->retain();
this->addChild(_label3); _label4 = Label::createWithTTF(ttfConfig, "Projection: 0");
_label4->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
_label4->setPosition(30,30);
_label4->setPosition(30,s.height/2 - 60); this->addChild(_label4); _count1 = 0;
_count2 = 0;
_count3 = 0;
_count4 = 0; auto dispatcher = Director::getInstance()->getEventDispatcher(); auto event1 = dispatcher->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&HelloWorld::onEvent1, this,std::placeholders::_1));
auto event2 = dispatcher->addCustomEventListener(Director::EVENT_AFTER_VISIT, std::bind(&HelloWorld::onEvent2, this,std::placeholders::_1));
auto event3 = dispatcher->addCustomEventListener(Director::EVENT_AFTER_DRAW, [&](EventCustom* event){
char buf[20];
snprintf(buf, sizeof(buf) - 1, "Draw: %d",_count3++);
_label3->setString(buf); });
event1->retain();
event2->retain();
event3->retain(); scheduleUpdate(); return true;
} void HelloWorld::update(float delta)
{
static float time = 0; time += delta;
if(time > 0.5) {
Director::getInstance()->setProjection(Director::Projection::_2D);
time = 0;
}
} void HelloWorld::onEvent1(EventCustom *event)
{
char buf[20];
snprintf(buf, sizeof(buf) - 1, "Update: %d",_count1++); _label1->setString(buf);
}
void HelloWorld::onEvent2(EventCustom *event)
{
char buf[20];
snprintf(buf, sizeof(buf)-1, "Visit: %d", _count2++);
_label2->setString(buf);
}

Cocos2d-x3.0 触摸事件的更多相关文章

  1. cocos2d 3.0自定义事件答疑解惑

    疑惑一:在事件分发中修改订阅者 ,对于这个的理解. 事件的分发是可以嵌套的,cocos2dx使用_inDispatch来保存当前嵌套的深度,当调用第一个dispatchEvent的时候,_inDisp ...

  2. [iOS UI进阶 - 3.0] 触摸事件的基本处理

    A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...

  3. Android: 详解触摸事件如何传递

    当视图的层次结构比较复杂的时候,触摸事件的响应流程也变得复杂. 举例来说,你也许有一天想要制作一个手势极其复杂的 Activity 来折磨你的用户,你经过简单思索,认为其中应该包含一个 PageVie ...

  4. [置顶] 使用Android OpenGL ES 2.0绘图之六:响应触摸事件

    传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...

  5. cocos2d-x触摸事件优先级的探究与实践

    如何让自定义Layer触发触摸事件? bool LayerXXX::init() { this->setTouchEnabled(true); CCTouchDispatcher* td = C ...

  6. cocos2d-x ios游戏开发初认识(八) 触摸事件与碰撞检測

    玩过植物大战僵尸都知道,要在草坪里放一朵向日葵或者其他的植物仅仅需触摸那个植物将其拖入到想要摆放的位置,这事实上就是这节要写的触摸事件.还能够发现当我们的僵尸出来的时候,我们的小豌豆会发子弹攻击僵尸, ...

  7. Cocos2d-x示例:单点触摸事件

    为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例.使用事件触发器实现单点触摸事件.该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们. ...

  8. cocos2d-x触摸事件优先级

     CCTouchDispatcher是管理cocos2d-x中全部Touch事件派发的类, CCTouchDispatcher中包括了两个CCTouchHandler的列表, 分别存储Standa ...

  9. registerWithTouchDispatcher 注册单点触摸事件

    Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...

随机推荐

  1. gulp一般使用

    gulp的基本使用总结了一下几点: 1.gulp-ejs的使用 [ file include,html文件合并 ]: let ejs = require("gulp-ejs"); ...

  2. 【MFC】基于OpenCV的魔镜

    最近半个月事情太多了,参加了泰迪杯数据挖掘,参加学院的科技节,科技节里面总共我参加了数学建模.PS.软件设计制作.电子设计大赛这4个.还有期中考.英语论文作业.今天终于忙的差不多,有时间来总结一下前段 ...

  3. MongoDB安装(windows 10环境)

    1. 下载 https://www.mongdb.com/ 选择版本:ws 2008 R2 64bit 2. 安装运行下载完成的MSI文件 安装时候选择自定义配置,选择安装路径,安装到E盘 等待2分钟 ...

  4. java环境变量和tomcat环境变量配置

    一.JAVA环境变量的配置1.首先下载JDK JDK可以在Oracle(甲骨文)公司的官方网站http://www.oracle.com下载2.安装完成后查看JDK安装路径一般是C:\Program ...

  5. js实际工作中的技能点

    1.基础知识 a.原型,原型链 b.作用域,闭包 c.异步,单线程 2.JS API a.DOM操作 b.Ajax c.事件绑定 3.开发环境 a.版本管理(git) b.模块化(require.js ...

  6. DLL的导出函数重定向机制

    曾经,调试时跟进HeapAlloc,结果发现直接进入到ntdll的RtlAllocateHeap中,感到很有趣,就使用Dependency Walker查看kernel32.dll的导出函数,结果发现 ...

  7. Gotorch - 多机定时任务管理系统

    * { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...

  8. IT连创业系列:App产品上线后,运营怎么搞?(中)

    等运营篇写完,计划是想写一个IOS系列,把IT连App里用到和遇到的坑都完整的和大伙分享. 不过写IOS系列前,还是要认真把这个运营篇写完,接下来好好码字!!! 上篇说到,我们计划去一次富士康门口,拉 ...

  9. 深入了解Android蓝牙Bluetooth——《基础篇》

    什么是蓝牙?   也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用"蓝牙"技术,能够有效地简化掌上电脑.笔记本电 ...

  10. js学习笔记(延时器)

    //setTimeout()   //功能:设置一个延时器   //语法:var timer = window.setTimeout(code,millisec);   //参数: code:是任何合 ...