您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 开启多触点

2016-06-06 11:29 477 查看
添加委托

addStandardDelegate();

开启多点触控

setTouchEnabled();

触屏事件:

1.注册多点触控

virtual void registerWithTouchDispatcher(void);

2.当用户第一次触碰手机屏幕时响应的回调函数

virtual void ccTouchesBegan(CCSet * touchs,CCEvent * event);

3.当用户手指在手机屏幕上滑动时响应的回调函数

virtual void ccTouchesMoved(CCSet * touchs,CCEvent * event);

4.当用户手指在离开手机屏幕上时响应的回调函数

virtual void ccTouchesEnded(CCSet * touchs,CCEvent * event);

5.在AppController.mm的didFinishLaunchingWithOptions方法中添加开启多触点代码。如下:

[cpp] view
plain copy

 





- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

  

    // Override point for customization after application launch.  

  

    // Add the view controller's view to the window and display.  

    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];  

    EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]  

                                     pixelFormat: kEAGLColorFormatRGBA8  

                                     depthFormat: GL_DEPTH_COMPONENT16  

                              preserveBackbuffer: NO  

                                      sharegroup: nil  

                                   multiSampling: NO  

                                 numberOfSamples:0 ];  

    [__glView setMultipleTouchEnabled:YES]; //添加这句代码  

    // Use RootViewController manage EAGLView  

    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];  

    viewController.wantsFullScreenLayout = YES;  

    viewController.view = __glView;  

  

    // Set RootViewController to window  

    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)  

    {  

        // warning: addSubView doesn't work on iOS6  

        [window addSubview: viewController.view];  

    }  

    else  

    {  

        // use this method on ios6  

        [window setRootViewController:viewController];  

    }  

      

    [window makeKeyAndVisible];  

  

    [[UIApplication sharedApplication] setStatusBarHidden: YES];  

  

    cocos2d::CCApplication::sharedApplication()->run();  

    return YES;  

}  

.cpp简单案例:

[cpp] view
plain copy

 





#include "HelloWorldScene.h"  

#include "SimpleAudioEngine.h"  

  

using namespace cocos2d;  

using namespace CocosDenshion;  

  

CCScene* HelloWorld::scene()  

{  

    CCScene *scene = CCScene::create();  

    HelloWorld *layer = HelloWorld::create();  

    scene->addChild(layer);  

    return scene;  

}  

bool HelloWorld::init()  

{  

    if ( !CCLayer::init() )  

    {  

        return false;  

    }  

      

    //开启多点触控  

    this->setTouchEnabled(true);  

      

    return true;  

}  

  

//注册多点触控  

void HelloWorld::registerWithTouchDispatcher(void)  

{  

    //注册监听  

    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);  

}  

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)  

{  

    CCSetIterator it;  

    CCTouch* touch;  

      

    //这里的it相当于系统分配的数字,  

    //pTouches->begin()获取第一个数字  

    //pTouches->end()就是最后一位  

    for( it = pTouches->begin(); it != pTouches->end(); it++)  

    {  

        touch = (CCTouch*)(*it);  

          

        if(!touch)  

            break;  

          

        CCLog("Began id == %d",touch->getID());  

    }  

}  

void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)  

{  

    CCSetIterator it;  

    CCTouch* touch;  

      

    for( it = pTouches->begin(); it != pTouches->end(); it++)  

    {  

        touch = (CCTouch*)(*it);  

          

        if(!touch)  

            break;  

          

        CCLog("Moved id == %d",touch->getID());  

    }  

}  

void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)  

{  

    CCSetIterator it;  

    CCTouch* touch;  

      

    for( it = pTouches->begin(); it != pTouches->end(); it++)  

    {  

        touch = (CCTouch*)(*it);  

          

        if(!touch)  

            break;  

          

        CCLog("Ende id == %d",touch->getID());  

    }  

}  

  

//写上生命周期函数  

void HelloWorld::onEnter()  

{  

    CCLayer::onEnter();  

}  

void HelloWorld::onExit()  

{  

    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  

    CCLayer::onExit();  

}  

添加委托

addStandardDelegate();

开启多点触控

setTouchEnabled();

触屏事件:

1.注册多点触控

virtual void registerWithTouchDispatcher(void);

2.当用户第一次触碰手机屏幕时响应的回调函数

virtual void ccTouchesBegan(CCSet * touchs,CCEvent * event);

3.当用户手指在手机屏幕上滑动时响应的回调函数

virtual void ccTouchesMoved(CCSet * touchs,CCEvent * event);

4.当用户手指在离开手机屏幕上时响应的回调函数

virtual void ccTouchesEnded(CCSet * touchs,CCEvent * event);

5.在AppController.mm的didFinishLaunchingWithOptions方法中添加开启多触点代码。如下:

[cpp] view
plain copy

 





- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

  

    // Override point for customization after application launch.  

  

    // Add the view controller's view to the window and display.  

    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];  

    EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]  

                                     pixelFormat: kEAGLColorFormatRGBA8  

                                     depthFormat: GL_DEPTH_COMPONENT16  

                              preserveBackbuffer: NO  

                                      sharegroup: nil  

                                   multiSampling: NO  

                                 numberOfSamples:0 ];  

    [__glView setMultipleTouchEnabled:YES]; //添加这句代码  

    // Use RootViewController manage EAGLView  

    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];  

    viewController.wantsFullScreenLayout = YES;  

    viewController.view = __glView;  

  

    // Set RootViewController to window  

    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)  

    {  

        // warning: addSubView doesn't work on iOS6  

        [window addSubview: viewController.view];  

    }  

    else  

    {  

        // use this method on ios6  

        [window setRootViewController:viewController];  

    }  

      

    [window makeKeyAndVisible];  

  

    [[UIApplication sharedApplication] setStatusBarHidden: YES];  

  

    cocos2d::CCApplication::sharedApplication()->run();  

    return YES;  

}  

.cpp简单案例:

[cpp] view
plain copy

 





#include "HelloWorldScene.h"  

#include "SimpleAudioEngine.h"  

  

using namespace cocos2d;  

using namespace CocosDenshion;  

  

CCScene* HelloWorld::scene()  

{  

    CCScene *scene = CCScene::create();  

    HelloWorld *layer = HelloWorld::create();  

    scene->addChild(layer);  

    return scene;  

}  

bool HelloWorld::init()  

{  

    if ( !CCLayer::init() )  

    {  

        return false;  

    }  

      

    //开启多点触控  

    this->setTouchEnabled(true);  

      

    return true;  

}  

  

//注册多点触控  

void HelloWorld::registerWithTouchDispatcher(void)  

{  

    //注册监听  

    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);  

}  

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)  

{  

    CCSetIterator it;  

    CCTouch* touch;  

      

    //这里的it相当于系统分配的数字,  

    //pTouches->begin()获取第一个数字  

    //pTouches->end()就是最后一位  

    for( it = pTouches->begin(); it != pTouches->end(); it++)  

    {  

        touch = (CCTouch*)(*it);  

          

        if(!touch)  

            break;  

          

        CCLog("Began id == %d",touch->getID());  

    }  

}  

void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)  

{  

    CCSetIterator it;  

    CCTouch* touch;  

      

    for( it = pTouches->begin(); it != pTouches->end(); it++)  

    {  

        touch = (CCTouch*)(*it);  

          

        if(!touch)  

            break;  

          

        CCLog("Moved id == %d",touch->getID());  

    }  

}  

void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)  

{  

    CCSetIterator it;  

    CCTouch* touch;  

      

    for( it = pTouches->begin(); it != pTouches->end(); it++)  

    {  

        touch = (CCTouch*)(*it);  

          

        if(!touch)  

            break;  

          

        CCLog("Ende id == %d",touch->getID());  

    }  

}  

  

//写上生命周期函数  

void HelloWorld::onEnter()  

{  

    CCLayer::onEnter();  

}  

void HelloWorld::onExit()  

{  

    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  

    CCLayer::onExit();  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: