加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x_多点触摸

发布时间:2020-12-14 19:11:21 所属栏目:百科 来源:网络整理
导读://// 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 cocos2
//
// 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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读