Cocos2d-x_多点触摸

前端之家收集整理的这篇文章主要介绍了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,91);
    
    CCSprite *pSpr2 = CCSprite::create("Icon-57.png");
    pSpr2->setPosition(ccp(150,200));
    this->addChild(pSpr2,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");
}
原文链接:https://www.f2er.com/cocos2dx/346744.html

猜你在找的Cocos2d-x相关文章