多触点

新建工程名为testMutiTouch

修改HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"

using namespace cocos2d;

class HelloWorld : public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

    virtual bool init();


    // there's no 'id' in cpp, so we recommend to return the class instance pointer

    static cocos2d::CCScene* scene();    

  

    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

    

    //重写多触点回调函数

    virtual void registerWithTouchDispatcher(void);

    virtual void ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);

    virtual void ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);

    virtual void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);

    

    //生命周期函数

    virtual void onExit();

};


#endif // __HELLOWORLD_SCENE_H__


修改HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


using namespace cocos2d;

using namespace CocosDenshion;


CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

    return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    //开启多触点监听务必调用此函数

    setTouchEnabled(true);


    CCSprite *sp1=CCSprite::create("Icon.png");

    sp1->setColor(ccc3(255, 255, 0));//便于区分

    CCSprite *sp2=CCSprite::create("Icon.png");

    sp1->setPosition(ccp(150,100));

    sp2->setPosition(ccp(150,200));

    addChild(sp1,0,91);

    addChild(sp2,0,92);

    

    return true;

}



//重写多触点回调函数

void HelloWorld::registerWithTouchDispatcher(void)

{

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

}

//用户手指第一次触碰

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

{

    CCSetIterator iter=pTouches->begin();

    for (;iter!=pTouches->end(); iter++)

    {

        CCTouch* pTouch=(CCTouch*)(*iter);

        CCPoint location=pTouch->getLocation();

        if (pTouch->getID()==0)

        {//第一个触点

            CCSprite *sp1=(CCSprite*) this->getChildByTag(91);

            sp1->setPosition(location);

        }

        else if(pTouch->getID()==1)//第二个触点

        {

            CCSprite *sp2=(CCSprite*) this->getChildByTag(92);

            sp2->setPosition(location);

        }        

    }

}

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

{

    CCSetIterator iter=pTouches->begin();

    for (;iter!=pTouches->end(); iter++)

    {

        CCTouch* pTouch=(CCTouch*)(*iter);

        CCPoint location=pTouch->getLocation();

        if (pTouch->getID()==0)

        {//第一个触点

            CCSprite *sp1=(CCSprite*) this->getChildByTag(91);

            sp1->setPosition(location);

        }

        else if(pTouch->getID()==1)//第二个触点

        {

            CCSprite *sp2=(CCSprite*) this->getChildByTag(92);

            sp2->setPosition(location);

        }

    }

}

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

{

    CCSetIterator iter=pTouches->begin();

    for (;iter!=pTouches->end(); iter++)

    {

        CCTouch* pTouch=(CCTouch*)(*iter);

        CCPoint location=pTouch->getLocation();

        CCLOG("pTouch 触摸点 i 的坐标: x:%f,y:%f",pTouch->getID(),location.x,location.y);

    }

}


//生命周期函数

void HelloWorld::onExit()

{

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

    CCLayer::onExit();

}

修改AppController.mm

- (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];


你可能感兴趣的:(多触点)