touch
事件监听共有五种事件
- 触摸事件 (EventListenerTouch) 单点触摸 \/ 多点触摸
- 键盘响应事件 (EventListenerKeyboard)
- 响应加速器事件 (EventListenerAcceleration)
- 鼠标响应事件 (EventListenerMouse)
- 自定义事件 (EventListenerCustom)
touch
/** returns the current touch location in OpenGL coordinates */ Vec2 getLocation() const;//获取当前touch位置,该位置基于OpenGL坐标 /** returns the previous touch location in OpenGL coordinates */ Vec2 getPreviousLocation() const;//获取前一次touch位置,该位置基于OpenGL坐标 /** returns the start touch location in OpenGL coordinates */ Vec2 getStartLocation() const;//获取该touch的起始位置,该位置基于OpenGL坐标 /** returns the delta of 2 current touches locations in screen coordinates */ Vec2 getDelta() const; //获取前后两次位置的偏移量,基于OpenGL坐标 /** returns the current touch location in screen coordinates */ Vec2 getLocationInView() const; //当前touch位置,该位置基于屏幕坐标位置 /** returns the previous touch location in screen coordinates */ Vec2 getPreviousLocationInView() const; //获取touch前一次的位置,基于屏幕坐标位置 /** returns the start touch location in screen coordinates */ Vec2 getStartLocationInView() const; //获取touch起始位置,基于屏幕坐标位置
单点触摸
/** 创建一个触摸监听 */ auto myListener = EventListenerTouchOneByOne::create(); //单点触摸
// auto myListener = EventListenerTouchAllAtOnce::create(); //多点触摸 myListener->setSwallowTouches(false); //触摸穿透效果了, 是否想向下继续传递触摸 myListener->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 begin.....x:%f, y:%f", locationInNode.x, locationInNode.y); target->setOpacity(180); return true; } return false; }; myListener->onTouchMoved = [](Touch *touch, Event *event){ auto target = static_cast<Sprite *>(event->getCurrentTarget()); log("sprite moved.....x:%f, y:%f", touch->getLocation().x, touch->getLocation().y); /** getDelta() @return 获取前后两次位置的偏移量,基于OpenGL坐标 */ target->setPosition(target->getPosition() + touch->getDelta()); }; myListener->onTouchEnded = [](Touch *touch, Event *event){ auto target = static_cast<Sprite *>(event->getCurrentTarget()); target->setOpacity(255); }; //添加监听器 _eventDispatcher 是node的属性,管理当前节点所有事件的分发 //注意:当再次使用 myListener 的时候,需要使用clone()方法创建一个新的克隆,因为在使用addEventListenerWithSceneGraphPriority或者addEventListenerWithFixedPriority方法时,会对当前使用的事件监听器添加一个已注册的标记,这使得它不能够被添加多次。另外,有一点非常重要,FixedPriority listener添加完之后需要手动remove,而SceneGraphPriority listener是跟Node绑定的,在Node的析构函数中会被移除 _eventDispatcher->addEventListenerWithSceneGraphPriority(myListener, myCard[0]); _eventDispatcher->addEventListenerWithSceneGraphPriority(myListener->clone(), myCard[1]);
多点触摸
auto myListener = EventListenerTouchAllAtOnce::create(); myListener->onTouchesBegan = [=](const std::vector<Touch *>&touches, Event *unused_event){ for (auto &item:touches) { auto *sprite = Sprite::create("sprite.png"); sprite->setPosition(item->getLocation()); sprite->setOpacity(120); this->addChild(sprite); } }; myListener->onTouchesMoved = [=](const std::vector<Touch *>&touches, Event *unused_event){ for (auto &item:touches) { int id = item->getID(); auto array = this->getChildren(); auto sprite = array.at(id); auto point = item->getLocation(); sprite->setPosition(point); log("id = %d\tx= %f\ty = %f", id, point.x, point.y); } }; myListener->onTouchesEnded = [=](const std::vector<Touch *>touches, Event *unused_event){ for (auto &item : touches) { int id = item->getID(); auto array = this->getChildren(); auto sprite = array.at(id); sprite->setOpacity(255); auto point = item->getLocation(); sprite->setPosition(point); log("id = %d\tx = %f\ty = %f", id, point.x, point.y); } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(myListener, this);
鼠标
auto myMouseListener = EventListenerMouse::create(); myMouseListener->onMouseMove = [=](Event *event) { EventMouse* e = (EventMouse*)event; this->mouse->setPosition(e->getCursorX(), e->getCursorY() + visibleSize.height); }; myMouseListener->onMouseDown = [=](Event *event) { this->mouse->setScale(0.2f); }; myMouseListener->onMouseUp = [=](Event *event) { this->mouse->setScale(0.3f); }; _eventDispatcher->addEventListenerWithSceneGraphPriority(myMouseListener, this);
键盘
auto myKeyListener = EventListenerKeyboard::create(); myKeyListener->onKeyPressed = [](EventKeyboard::KeyCode keycode, cocos2d::Event *event) { CCLOG("key is pressed,keycode is %d",keycode); }; myKeyListener->onKeyReleased = [](EventKeyboard::KeyCode keycode, cocos2d::Event *event) { CCLOG("key is released,keycode is %d", keycode); }; _eventDispatcher->addEventListenerWithSceneGraphPriority(myKeyListener, this);
加速器
sprite = Sprite::create("sprite.png"); sprite->setPosition(screenWidth/2, screenHeight/2); this->addChild(sprite); Device::setAccelerometerEnabled(true); auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(NextScene::onAcceleration, this)); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); void NextScene::onAcceleration(Acceleration *acc, Event *event) { auto point = this->sprite->getPosition(); if (acc->x < 0 && point.x>0) { point.x--; }else if (acc->x > 0 && point.x>0){ point.x++; } if (acc->y < 0 && point.x>0) { point.y--; } else if (acc->y > 0 && point.y<this->screenHeight) { point.y++; } if (acc->z<0 && this->scale>0.1) { this->scale = this->scale*0.9; } else if (acc->z>0 && this->scale < 10) { this->scale = this->scale*1.1; } this->sprite->setPosition(point); this->sprite->setScale(0.3*this->scale); }