分享

cocos2d-x-3.1 事件分发机制 (coco2d-x 学习笔记七)

 代码也是种艺术 2016-02-25


触摸事件

  1. Sprite* sp1 = Sprite::create("Images/t1.png");  
  2. sp1->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));  
  3. addChild(sp1, 10,1);  
  4.   
  5. auto mTouchListener = EventListenerTouchOneByOne::create();  //单点触摸事件  
  6. mTouchListener->setSwallowTouches(true);   //true向下传递  
  7.   
  8. mTouchListener->onTouchBegan = [](Touch* touch, Event* event){  //匿名方式设置事件  
  9. Sprite* target = static_cast<Sprite*>(event->getCurrentTarget());  
  10.   
  11. Point locationInNode = target->convertToNodeSpace(touch->getLocation());  
  12. Size s = target->getContentSize();  
  13. Rect normal = Rect(0, 0, s.width, s.height);  
  14. if (normal.containsPoint(locationInNode)){  
  15.     log("x=%f,y=%f", locationInNode.x, locationInNode.y);  
  16.     target->setOpacity(0x7F);   //设置透明度  
  17.     return true;                //向下传递事件  
  18.     }  
  19.     return false;  
  20. };  
  21.   
  22. //绑定方式设置事件 其onTouchBegan函数的内容和匿名方式内容一样  
  23. //listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);    
  24.   
  25. mTouchListener->onTouchMoved = [](Touch* touch, Event* event){};  //触摸移动监听  
  26. mTouchListener->onTouchEnded = [](Touch* touch, Event* event){};  //触摸监听结束  
  27.   
  28. /* 
  29. _eventDispatcher是Node的属性,通过它管理当前节点(场景、层、精灵等)的所有事件的分发。 
  30. 但它本身是一个单例模式值的引用,在Node的构造函数中, 
  31. 通过Director::getInstance()->getEventDispatcher(); 获取,有了这个属性,就能方便的处理事件。 
  32. */  
  33. //将触摸事件交给事件分发器管理  
  34.   
  35. _eventDispatcher->addEventListenerWithSceneGraphPriority(mTouchListener, sp1);    
  36.   
  37. /*ps:当再次使用 mTouchListener 的时候,需要使用clone()方法创建一个新的克隆。因为在使用addEventListenerWithSceneGraphPriority方法时,会对当前使用的事件监听器添加一个已注册的标记,这使得它不能够被添加多次。另外,有一点非常重要mTouchListener是跟Node绑定的,在Node的析构函数中会被移除。 
  38. */  





键盘响应事件

  1. auto mKeyboardListener = EventListenerKeyboard::create();  //键盘响应事件  
  2.   
  3. //键盘按下事件监听  
  4. mKeyboardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){  
  5.     log("keyCode=%d", keyCode);  
  6. };  
  7. //键盘释放事件监听  
  8. mKeyboardListener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){  
  9.     log("keyCode=%d", keyCode);  
  10. if (EventKeyboard::KeyCode::KEY_ESCAPE == keyCode){  
  11. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)  
  12.     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");  
  13.     return;  
  14. #endif  
  15.     Director::getInstance()->end();  
  16.   
  17. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  18.     exit(0);  
  19. #endif  
  20.     }  
  21. };  
  22.   
  23. _eventDispatcher->addEventListenerWithSceneGraphPriority(mKeyboardListener, this);  //将键盘响应事件交给分发器来管理   


鼠标响应事件

  1. auto _mouseListener = EventListenerMouse::create();  
  2.   
  3. // 事件响应逻辑  
  4. _mouseListener->onMouseMove = [=](Event *event){  
  5.     EventMouse* e = (EventMouse*)event;  
  6.     log("Key=%d", e->getMouseButton());  
  7. };  
  8. _mouseListener->onMouseUp = [=](Event *event){};  
  9. _mouseListener->onMouseDown = [=](Event *event){};  
  10. _mouseListener->onMouseScroll = [=](Event *event){};  
  11. // 添加到事件分发器  
  12. _eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);  


自定义事件


  1. auto mCustomListener = EventListenerCustom::create("my_custom_l", [](EventCustom* event){  
  2. char* c = static_cast<char*>(event->getUserData());  
  3. log("%s", c);  
  4. });  
  5. /* 
  6. 将自自定义事件交给分发器来管理,这种方式是使用优先级别来设置 
  7. SceneGraphPriority和FixedPriority区别在于前者是在析构函数中会被移除 
  8. 后者是需要手动移除 
  9. */_eventDispatcher->addEventListenerWithFixedPriority(mCustomListener, 1);  
  10.   
  11. EventCustom _event("my_custom_l");  
  12. //char* cstr = "this is my custom listener!";  
  13.   
  14. static int count = 0;  
  15. ++count;  
  16. char* buf;  
  17. sprintf(buf, "this is my custom %d", count);  
  18.   
  19. _event.setUserData(buf);  
  20.   
  21. _eventDispatcher->dispatchEvent(&_event);  //手动触发自定义事件  




加速计事件
  1. //启动加速硬件设备  
  2. Device::setAccelerometerEnabled(true);  
  3.   
  4. auto mAcListener = EventListenerAcceleration::create([](Acceleration* acc, Event* event){  
  5. #define FIX_POS(_pos, _min, _max) \  
  6. if (_pos < _min)        \  
  7.     _pos = _min;        \  
  8. else if (_pos > _max)   \  
  9.     _pos = _max;        \  
  10. //log("x=%lf,y=%lf", acc->x, acc->y);  
  11.   
  12.     auto ballSize = sp1->getContentSize();  
  13.   
  14.     auto ptNow = sp1->getPosition();  
  15.   
  16.     ptNow.x += acc->x * 9.81f;  
  17.     ptNow.y += acc->y * 9.81f;  
  18.   
  19.     FIX_POS(ptNow.x, (VisibleRect::left().x + ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));  
  20.     FIX_POS(ptNow.y, (VisibleRect::bottom().y + ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));  
  21.     sp1->setPosition(ptNow);  
  22. });  
  23.   
  24. _eventDispatcher->addEventListenerWithSceneGraphPriority(mAcListener, this);  





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多