cocos2dx-触摸、传感器、物理按键及绘图(四)

前端之家收集整理的这篇文章主要介绍了cocos2dx-触摸、传感器、物理按键及绘图(四)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这些都是添加在init()方法


1.单点触摸事件监听


2.多点触摸

    //多点触摸
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan=[](std::vector<Touch*> ts,Event *e){
        log("ontouchbegin");
    };
    //移动
    listener->onTouchesMoved=[](std::vector<Touch*> ts,Event *e){
        log("touches moved and counts is %ld",ts.size());
    };
    
    //this表示这个层
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);


3.加速传感器
//添加加速度传感器
    //1.打开传感器
    Device::setAccelerometerEnabled(true);
    //2.监听传感器
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(EventListenerAcceleration::create([](Acceleration *a,Event *e){
        log("x:%g,y:%g,z:%g",a->x,a->y,a->z);
        
    }),this);

4.物理按键
   //物理按键监听
    auto listener = EventListenerKeyboard::create();
    listener->onKeyReleased=[](EventKeyboard::KeyCode code,Event*e){
        log("key code :%d",code);
        switch (code) {
            case EventKeyboard::KeyCode::KEY_BACKSPACE:
                Director::getInstance()->end();
                break;
            case EventKeyboard::KeyCode::KEY_MENU:
                log("this is menu");
            default:
                break;
        }
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);

5.绘图

    auto r = cocos2d::DrawNode::create();
    addChild(r);
//    r->drawDot(Vec2(100,100),10,cocos2d::Color4F::GREEN);
    r->drawRect(Point(20,10),Point(100,Color4F::RED);
    r->drawSolidRect(Point(120,120),Point(500,500),Color4F::BLUE);

猜你在找的Cocos2d-x相关文章