Cocos2d-x_多点触摸

//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();
    
    CREATE_FUNC(HelloWorld);
    
    // 重写“多点”触摸回调函数
    virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
    virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
    
    // 生命周期函数
    virtual void OnEnter();
    virtual void OnExit();
    
};

#endif
//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    // 在AppController.mm文件中开启ios“多点”触摸支持功能
    // [__glView setMultipleTouchEnabled:YES];
    
    // 开启“多点”触摸监听代理
    this->setTouchEnabled(true);
    
    CCSprite *pSpr1 = CCSprite::create("Icon-57.png");
    pSpr1->setColor(ccc3(255, 255, 0));
    pSpr1->setPosition(ccp(150, 100));
    this->addChild(pSpr1,0,91);
    
    CCSprite *pSpr2 = CCSprite::create("Icon-57.png");
    pSpr2->setPosition(ccp(150, 200));
    this->addChild(pSpr2,0,92);
    
    return true;
}

void HelloWorld::OnEnter()
{
    CCLayer::onEnter();
    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);  // addStandardDelegate 增加“多点”触摸代理
}

void HelloWorld::OnExit()
{
    CCLayer::onExit();
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchesBegan");
    
    CCSetIterator iter = pTouches->begin();
    for (; iter != pTouches->end(); iter ++)
    {
        CCTouch *pTouch = (CCTouch *)(*iter);
        CCPoint location = pTouch->getLocation();
        if (pTouch->getID() == 0)
        {
            CCSprite *pSpr1 = (CCSprite *)this->getChildByTag(91);
            pSpr1->setPosition(location);
        }
        else if(pTouch->getID() == 1)
        {
            CCSprite *pSpr2 = (CCSprite *)this->getChildByTag(92);
            pSpr2->setPosition(location);
        }
    }
}

void HelloWorld::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchesMoved");
    
    CCSetIterator iter = pTouches->begin();
    for (; iter != pTouches->end(); iter ++)
    {
        CCTouch *pTouch = (CCTouch *)(*iter);
        CCPoint location = pTouch->getLocation();
        if (pTouch->getID() == 0)
        {
            CCSprite *pSpr1 = (CCSprite *)this->getChildByTag(91);
            pSpr1->setPosition(location);
        }
        else if(pTouch->getID() == 1)
        {
            CCSprite *pSpr2 = (CCSprite *)this->getChildByTag(92);
            pSpr2->setPosition(location);
        }
    }
}

void HelloWorld::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchesEnded");
    
    CCSetIterator iter = pTouches->begin();
    for (; iter != pTouches->end(); iter ++)
    {
        CCTouch *pTouch = (CCTouch *)(*iter);
        CCPoint location = pTouch->getLocation();
        CCLOG("pTouch触摸点%d的坐标:x:%f,y:%f",pTouch->getID(),location.x, location.y);
    }
}

void HelloWorld::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCLOG("HelloWorld::ccTouchesCancelled");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值