多触点

新建工程名为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];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值