单点触屏事件:
//先创建一个单点触屏监听器 auto listener = EventListenerTouchOneByOne::create(); //注册监听器的回调方法,分别回调本节点的三个方法 listener->onTouchBegan = CC_CALLBACK_2(EventScene::onTouchBegan,this); listener->onTouchEnded = CC_CALLBACK_2(EventScene::onTouchEnded,this); listener->onTouchMoved = CC_CALLBACK_2(EventScene::onTouchMoved,this); //注册监听器 _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);
//手势开始的时候执行 bool EventScene::onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *unused_event) { //获取屏幕触控点 auto p = touch->getLocationInView(); //把点的转换为GL坐标 p = Director::getInstance()->convertToGL(p); //在控制台输出点的坐标 CCLOG("[%f,%f]",p.x,p.y); //如果返回false,就不再往下执行,后面的onTouchMoved和onTouchEnded就不会再执行 return true; } //滑动手势的时候执行 void EventScene::onTouchMoved(cocos2d::Touch *touch,cocos2d::Event *unused_event) { CCLOG("执行移动操作"); } //手势结束的时候执行 void EventScene::onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *unused_event) { CCLOG("手势结束了"); }
多点触屏事件:
//注册监听器的回调方法,分别回调本节点的三个方法 //与单点触控是不一样的方法 auto all_listener = EventListenerTouchAllAtOnce::create(); all_listener->onTouchesBegan = CC_CALLBACK_2(EventScene::onTouchesBegan,this); all_listener->onTouchesMoved = CC_CALLBACK_2(EventScene::onTouchesMoved,this); all_listener->onTouchesEnded = CC_CALLBACK_2(EventScene::onTouchesEnded,this); //注册监听器 _eventDispatcher->addEventListenerWithSceneGraPHPriority(all_listener,this);
void EventScene::onTouchesBegan(const std::vector<Touch *> &touches,cocos2d::Event *unused_event) { //从触控的多点中,取出一个触控点 auto t = touches[0]; //获取屏幕触控点 auto p = t->getLocationInView(); //把点的转换为GL坐标 p = Director::getInstance()->convertToGL(p); //在控制台输出点的坐标 CCLOG("[%f,p.y); } void EventScene::onTouchesMoved(const std::vector<Touch *> &touches,cocos2d::Event *unused_event) { CCLOG("多点触控执行移动操作"); } void EventScene::onTouchesEnded(const std::vector<Touch *> &touches,cocos2d::Event *unused_event) { CCLOG("多点触控手势结束了"); }
鼠标事件:
auto mouse_listener = EventListenerMouse::create(); mouse_listener->onMouseUp = CC_CALLBACK_1(EventScene::onMouseUp,this); mouse_listener->onMouseDown = CC_CALLBACK_1(EventScene::onMouseDown,this); mouse_listener->onMouseScroll = CC_CALLBACK_1(EventScene::onMouseScroll,this); mouse_listener->onMouseMove = CC_CALLBACK_1(EventScene::onMouseMove,this); _eventDispatcher->addEventListenerWithSceneGraPHPriority(mouse_listener,this);
void EventScene::onMouseDown(cocos2d::Event *e) { EventMouse* em = (EventMouse*)e; int b = em->getMouseButton(); CCLOG("你点击的是鼠标的%d",b); } void EventScene::onMouseMove(cocos2d::Event *e) { EventMouse* em = (EventMouse*)e; CCLOG("鼠标移动的坐标[%f,em->getCursorX(),em->getCursorY()); } void EventScene::onMouseScroll(cocos2d::Event *e) { EventMouse* em = (EventMouse*)e; CCLOG("滑轮滚动的坐标[%f,em->getScrollX(),em->getScrollY()); } void EventScene::onMouseUp(cocos2d::Event *e) { CCLOG("鼠标抬起"); }
键盘事件:
auto keyboard_listener = EventListenerKeyboard::create(); keyboard_listener->onKeyReleased = CC_CALLBACK_2(EventScene::onKeyReleased,this); keyboard_listener->onKeyPressed = CC_CALLBACK_2(EventScene::onKeyPressed,this); _eventDispatcher->addEventListenerWithSceneGraPHPriority(keyboard_listener,this);
void EventScene::onKeyPressed(EventKeyboard::KeyCode keyCode,cocos2d::Event *event) { switch (keyCode) { case cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW: CCLOG("UP..."); break; case cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW: CCLOG("DOWN..."); break; case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW: CCLOG("RIGHT..."); break; case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW: CCLOG("LEFT..."); break; } } void EventScene::onKeyReleased(EventKeyboard::KeyCode keyCode,cocos2d::Event *event) { }
加速度事件:
//首先,要打开加速度传感器 Device::setAccelerometerEnabled(true); //创建一个加速度传感器监听器,参数是一个回调 auto acceler_listener = EventListenerAcceleration::create(CC_CALLBACK_2(EventScene::onAcceleration,this)); //注册监听器 _eventDispatcher->addEventListenerWithSceneGraPHPriority(acceler_listener,this);
void EventScene::onAcceleration(cocos2d::Acceleration *acc,cocos2d::Event *unused_event) { //分别获取x,y,z方向上的加速度 float x = acc->x; float y = acc->y; float z = acc->z; }