加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 20:14:11 所属栏目:百科 来源:网络整理
导读:这些都是添加在init()方法中 1.单点触摸事件监听 2.多点触摸 //多点触摸 auto listener = EventListenerTouchAllAtOnce::create(); listener-onTouchesBegan=[](std::vectorTouch* ts,Event *e){ log("ontouchbegin"); }; //移动 listener-onTouchesMoved=[]

这些都是添加在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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读