zoukankan      html  css  js  c++  java
  • cocos2d-x 多触点监听

    1. //首先到cocos2d-x项目下的ios目录下。找到AppController.mm文件,在函数 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中加入例如以下函数:  [__glView setMultipleTouchEnabled:YES];  
    2.   
    3. bool HelloWorld::init()  
    4. {  
    5.       
    6.     if ( !CCLayer::init() )  
    7.     {  
    8.         return false;  
    9.     }  
    10.       
    11.     //开启多触点监听务必调用此函数  
    12.     setTouchEnabled(true);  
    13.       
    14.     CCSprite* sp1 = CCSprite::create("Icon.png");  
    15.     sp1->setPosition(ccp(150, 200));  
    16.     addChild(sp1, 0, 23);  
    17.       
    18.     CCSprite* sp2 = CCSprite::create("Icon.png");  
    19.     sp2->setColor(ccc3(0, 255, 0));  
    20.     sp2->setPosition(ccp(150, 100));  
    21.     addChild(sp2, 0, 24);  
    22.       
    23.     return true;  
    24. }  
    25.   
    26. //第一次碰触  
    27. void HelloWorld::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *event)  
    28. {  
    29.     CCSetIterator inter = touches->begin();  
    30.     for(; inter != touches->end(); inter++)  
    31.     {  
    32.         CCTouch* touch = (CCTouch*)(*inter);  
    33.         CCPoint point = touch->getLocation();  
    34.         if(touch->getID() == 0) //第一个触点  
    35.         {  
    36.             CCSprite* sp1 = (CCSprite*)getChildByTag(23);  
    37.             sp1->setPosition(point);  
    38.         }else if(touch->getID() == 1)//第二个触点  
    39.         {  
    40.             CCSprite* sp2 = (CCSprite*)getChildByTag(24);  
    41.             sp2->setPosition(point);  
    42.         }  
    43.     }  
    44. }  
    45.   
    46. //移动或拖拽  
    47. void HelloWorld::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *event)  
    48. {  
    49.     CCSetIterator inter = touches->begin();  
    50.     for(; inter != touches->end(); inter++)  
    51.     {  
    52.         CCTouch* touch = (CCTouch*) (*inter);  
    53.         CCPoint point = touch->getLocation();  
    54.         if(touch->getID() == 0)  
    55.         {  
    56.             CCSprite* sp1 = (CCSprite*)getChildByTag(23);  
    57.             sp1->setPosition(point);  
    58.         }else if(touch->getID() == 1)  
    59.         {  
    60.             CCSprite* sp2 = (CCSprite*)getChildByTag(24);  
    61.             sp2->setPosition(point);  
    62.         }  
    63.     }  
    64. }  
    65.   
    66. //用户手指抬起  
    67. void HelloWorld::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *event)  
    68. {  
    69.       
    70. }  
    71.   
    72. //多触点的托付监听注冊放在onEnter的生命函数中会造成程序异常退出。默认都写在以下函数中。

        

    73. void HelloWorld::registerWithTouchDispatche()  
    74. {  
    75.     CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);  
    76. }  
    77.   
    78.   
    79. //删除多触点的托付监听  
    80. void HelloWorld::onExit()  
    81. {  
    82.     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  
    83.   
    84.     //这句务必要写  
    85.     CCLayer::onExit();  
    86. }  
  • 相关阅读:
    STS新建MavenProject后java文件夹不出来的问题
    tomcat版本的选择
    eclipse/sts快捷键
    STS使用前准备工作
    注释 @Autowired 和@Resource 的区别
    java获得项目绝对路径
    使用Run as --> maven install 报错
    本地仓库settings.xml中使用阿里的仓库
    Maven学习——安装与修改Maven的本地仓库路径
    Eclipse+Maven创建webapp项目
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7123223.html
Copyright © 2011-2022 走看看