cocos2d-x-3.3rc2 其他事件

来源:互联网 发布:换手率软件官网 编辑:程序博客网 时间:2024/06/15 09:08

_eventDispatcher 是 Node中定义的。

_touchListener和 _keyboardListener是使用类中定义 应该是为了移除事件



重力感应事件

    Device::setAccelerometerEnabled(true);    auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(PhysicsDemoClickAdd::onAcceleration, this));    _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this);


   
void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event){    static float prevX=0, prevY=0;    #define kFilterFactor 0.05f        float accelX = (float) acc->x * kFilterFactor + (1- kFilterFactor)*prevX;    float accelY = (float) acc->y * kFilterFactor + (1- kFilterFactor)*prevY;        prevX = accelX;    prevY = accelY;        auto v = Vec2( accelX, accelY);    v = v * 200;        if(_scene != nullptr)    {        _scene->getPhysicsWorld()->setGravity(v);    }}

单点触控事件

    // Adds Touch Event Listener    auto listener = EventListenerTouchOneByOne::create();    listener->setSwallowTouches(true);        listener->onTouchBegan = CC_CALLBACK_2(Box2DView::onTouchBegan, this);    listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this);    listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this);        _eventDispatcher->addEventListenerWithFixedPriority(listener, -10);    _touchListener = listener;    


bool Box2DView::onTouchBegan(Touch* touch, Event* event){    auto touchLocation = touch->getLocation();        auto nodePosition = convertToNodeSpace( touchLocation );    log("Box2DView::onTouchBegan, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);    return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y));}void Box2DView::onTouchMoved(Touch* touch, Event* event){    auto touchLocation = touch->getLocation();        auto nodePosition = convertToNodeSpace( touchLocation );        log("Box2DView::onTouchMoved, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);        m_test->MouseMove(b2Vec2(nodePosition.x,nodePosition.y));        }void Box2DView::onTouchEnded(Touch* touch, Event* event){    auto touchLocation = touch->getLocation();        auto nodePosition = convertToNodeSpace( touchLocation );        log("Box2DView::onTouchEnded, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);        m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y));}


按键事件

    auto keyboardListener = EventListenerKeyboard::create();    keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DView::onKeyPressed, this);    keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DView::onKeyReleased, this);    _eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, -11);    _keyboardListener = keyboardListener;

void Box2DView::onKeyPressed(EventKeyboard::KeyCode code, Event* event){    log("Box2dView:onKeyPressed, keycode: %d", code);    m_test->Keyboard(static_cast<unsigned char>(code));}void Box2DView::onKeyReleased(EventKeyboard::KeyCode code, Event* event){    log("onKeyReleased, keycode: %d", code);    m_test->KeyboardUp(static_cast<unsigned char>(code));}


删除事件

_eventDispatcher->removeEventListener(_touchListener);_eventDispatcher->removeEventListener(_keyboardListener);


0 0
原创粉丝点击