Cocos2d-x_多点触摸

Cocos2d-x_多点触摸,第1张

概述//// 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
//// 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");}
总结

以上是内存溢出为你收集整理的Cocos2d-x_多点触摸全部内容,希望文章能够帮你解决Cocos2d-x_多点触摸所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1005230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存