cocos2d-x 3.x学习之多点触摸

#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
private:
    double _distance;//两个触摸点之间的距离
    double _deltax;//目标x轴的改变值
    double _deltay;//目标y轴的改变值
    Sprite* _bgSprite;//目标精灵
    double _mscale;//初始地图缩放比例
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    void menuCloseCallback(cocos2d::Ref* pSender);
    void onTouchesBegan(const std::vector<Touch*>&touches,Event* event);
    void onTouchesMoved(const std::vector<Touch*>&touches,Event* event);
    void onTouchesEnded(const std::vector<Touch*>&touches,Event* event);
    void onTouchesCancelled(const std::vector<Touch*>&touches,Event* event);
    CREATE_FUNC(HelloWorld);
};
#include "HelloWorldScene.h"
USING_NS_CC;
/*
提示:如果在ios模拟器中进行测试,按着option键,屏幕上可出现两个触摸点,但是程序中始终
 只能得到一个点,即vector当中只有一个触摸点,这时需要修改ios目录下的AppController.mm文件,在文件中启用多点触摸。
 在AppController.mm的-(BOOL)application:(UIApplication*)application didFinishLauncing-WithOptions:(NSDictionary*)launchOptions函数最后增加
 一行代码
 激活多点触摸功能
 [eaglView setMultipleTouchEnabled:YES]
*/
Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    //标题标签
    auto title = Label::createWithSystemFont("多点触摸(缩放功能)测试","", 64);
    title->setPosition(visibleSize.width / 2,visibleSize.height * 0.9);
    this->addChild(title);
    //提示标签
    auto subtitle = Label::createWithSystemFont("请用手指缩放屏幕","", 32);
    subtitle->setPosition(visibleSize.width / 2,visibleSize.height * 0.8);
    this->addChild(subtitle);
    //使用HelloWorld.png创建一个精灵
    _bgSprite = Sprite::create("HelloWorld.png");
    _bgSprite->setPosition(Vec2(visibleSize.width / 2 + origin.x,visibleSize.height / 2 + origin.y));
    this->addChild(_bgSprite,0);
    //初始化图片缩放比例
    _mscale = 1.0;
    //创建多点触摸监听器
    auto listener = EventListenerTouchAllAtOnce::create();
    //绑定事件处理函数
    listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan,this);
    listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved,this);
    listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded,this);
    listener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled,this);
    //添加场景优先事件监听器
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    return true;
}
void HelloWorld::onTouchesBegan(const std::vector<Touch *> &touches, cocos2d::Event *event){
  //如果移动时触摸点的个数不少于两个
    if(touches.size() >= 2){
        //获得第一个触摸点
        auto touch1 = touches.at(0);
        Vec2 mPoint1 = Director::getInstance()->convertToGL(touch1->getLocation());
        //获得第二个触摸点
        auto touch2 = touches.at(1);
        Vec2 mPoint2 = Director::getInstance()->convertToGL(touch2->getLocation());
      //计算两个触摸点距离,开根号
        _distance = sqrt((mPoint2.x-mPoint1.x) * (mPoint2.x-mPoint1.x) + (mPoint2.y - mPoint1.x) * (mPoint2.y - mPoint1.y));
        
    }
}
void HelloWorld::onTouchesMoved(const std::vector<Touch *> &touches, cocos2d::Event *unused_event){
    //如果移动时触摸点的个数不少于两个
    if(touches.size() >= 2){
        //获得第一个触摸点
        auto touch1 = touches.at(0);
        Vec2 mPoint1 = Director::getInstance()->convertToGL(touch1->getLocation());
        //获得第二个触摸点
        auto touch2 = touches.at(1);
        Vec2 mPoint2 = Director::getInstance()->convertToGL(touch2->getLocation());
        //计算两个触摸点距离,开根号
        double mdistance = sqrt((mPoint2.x-mPoint1.x) * (mPoint2.x-mPoint1.x) + (mPoint2.y - mPoint1.x) * (mPoint2.y - mPoint1.y));
        // 新的距离 / 老的距离 * 原来的缩放比例,即为新的缩放比例
        _mscale = mdistance / _distance * _mscale;
        _distance = mdistance;
        //设置新的缩放比例
        _bgSprite->setScale(_mscale);
    }

}
void HelloWorld::onTouchesEnded(const std::vector<Touch *> &touches, cocos2d::Event *event){
}
void HelloWorld::onTouchesCancelled(const std::vector<Touch *> &touches, cocos2d::Event *event){
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值