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. 详细图解window环境mongodb下载、安装、配置与使用

    到官网下载最新版面mongodb安装包,(32位版本的已经取消了,只有64位的) 官网地址: https://www.mongodb.com/download-center#community 下载完 ...

  2. JavaScript核心参考

    Array 方法 concat() 把元素衔接到数组中. every() 测试断言函数是否对每个数组元素都为真. filter() 返回满足断言函数的数组元素. forEach() 为数组的每一个元素 ...

  3. Java 核心内容相关面试题【4】

    spingmvc 和 structs的区别 我们用struts2时采用的传统的配置文件的方式,并没有使用传说中的0配置. spring3 mvc可以认为已经100%零配置了(除了配置spring mv ...

  4. C#中访问私有成员

    首先访问一个类的私有成员不是什么好做法.大家都知道私有成员在外部是不能被访问的.一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员造访,可以套用下面这种非常好的方式去解决. pr ...

  5. Python之re正则模块二

    13.编译的标志 可以用re.I.re.M等参数,也可以直接在表达式中添加"?(iLmsux)"标志 *s:单行,“.”匹配包括换行符在内的所有字符 *i:忽略大小写 *L:让&q ...

  6. 浅谈Maven

    最近的有用到Maven 所以就大概跟大家谈一下Maven 1什么是Maven? Maven我个人的理解就是项目架包管理 通俗易懂的来说就是管理配置你项目中的所有jar包的 废话不多说 Maven去ht ...

  7. canvas图表(3) - 饼图

    原文地址:canvas图表(3) - 饼图 这几天把canvas图表都优化了下,动画效果更加出色了,可以说很逼近echart了.刚刚写完的饼图,非常好的实现了既定的功能,交互的动画效果也是很棒的. 效 ...

  8. Database differential backup差异备份和还原

    完整备份: 备份全部选中的文件和文件夹,并不依赖文件的存档属性来确定备份哪些文件.(在备份过程中,任何现有的标记都被清除,每个文件都被标记为已备份,换言之,清除存档属性),完全备份也叫完整备份. 差异 ...

  9. 《Linux命令行与shell脚本编程大全》第十五章 呈现数据

    15.1 理解输入和输出 现在知道两种显示脚本输出的方法 1)在显示器屏幕上显示 2)将输出文件重定向到文件中 15.1.1 标准文件描述符 Linux系统将每个对象当做文件处理.这包括输入和数出进程 ...

  10. 通过ELK快速搭建一个你可能需要的集中化日志平台

    在项目初期的时候,大家都是赶着上线,一般来说对日志没有过多的考虑,当然日志量也不大,所以用log4net就够了,随着应用的越来越多,日志散 落在各个服务器的logs文件夹下,确实有点不大方便,这个时候 ...