zoukankan      html  css  js  c++  java
  • cocos2d-x 3.x 触摸事件

    HelloWorldScene.h

       bool touchBegan(cocos2d::Touch*touch, cocos2d::Event*event);//触摸开始,返回bool类型
        void touchMoved(cocos2d::Touch*touch, cocos2d::Event*event);
        void touchEnded(cocos2d::Touch*touch, cocos2d::Event*event);
        void touchCancelled(cocos2d::Touch*touch, cocos2d::Event*event);

    HelloWorldScene.cpp

       EventListenerTouchOneByOne*listener=EventListenerTouchOneByOne::create();
        listener->onTouchBegan=CC_CALLBACK_2(HelloWorld::touchBegan,this);//触摸开始,必须添加,不使用也要添加,否则运行不通过
        listener->onTouchMoved=CC_CALLBACK_2(HelloWorld::touchMoved,this);//触摸移动
        listener->onTouchEnded=CC_CALLBACK_2(HelloWorld::touchEnded,this);//触摸中断,一般是系统层级的消息,如来电话,触摸事件就会被打断 
        listener->onTouchCancelled=CC_CALLBACK_2(HelloWorld::touchCancelled,this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
    
    
    
    
    bool HelloWorld::touchBegan(Touch*touch, Event*event){
        Vec2 pos=touch->getLocation();
        log("touchBegan x:%f,y:%f",pos.x,pos.y);
        return true;//返回false时,触摸移动、触摸结束不会触发
    }
    
    void HelloWorld::touchMoved(Touch*touch, Event*event){
        Vec2 pos=touch->getLocation();
        log("touchMoved x:%f,y:%f",pos.x,pos.y);
    }
    
    void HelloWorld::touchEnded(Touch*touch, Event*event){
        Vec2 pos=touch->getLocation();
        log("touchEnded x:%f,y:%f",pos.x,pos.y);
    }
    
    void HelloWorld::touchCancelled(Touch*touch, Event*event){
        Vec2 pos=touch->getLocation();
        log("touchCancelled x:%f,y:%f",pos.x,pos.y);
    }
  • 相关阅读:
    P1271 【深基9.例1】选举学生会(基数排序)
    P7076 [CSP-S2020] 动物园
    #10127. 「一本通 4.3 练习 1」最大数
    P2671 [NOIP2015 普及组] 求和
    P3369 【模板】普通平衡树
    P2503 [HAOI2006]均分数据
    P2846 [USACO08NOV]Light Switching G(动态开点写法)
    P6278 [USACO20OPEN]Haircut G
    P2341 [USACO03FALL][HAOI2006]受欢迎的牛 G
    P1012 [NOIP1998 提高组] 拼数
  • 原文地址:https://www.cnblogs.com/kingBook/p/5569607.html
Copyright © 2011-2022 走看看