cocos2d-x 3.x学习之多点触摸
2016-07-06 14:33
393 查看
#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 }
相关文章推荐
- Cocos2d-x 3.x新建项目
- Cocos2d-x 3.x新建项目
- cocos3.x新建项目
- 一些可能有用的功能cocos2dx
- cocos2dx通过SpriteFrame创建数字标签类
- 循环的pageview
- cocos2d spine 与 tmx相遇崩溃问题
- cocos2dx lua class 继承问题
- Quick-Cocos2D-X 并成功运行示例代码
- 使用CocosMeshViewer来转换和观察Cocos模型
- Cocos2d 碰撞检测函数
- Cocos2d-x 加载 csb资源文件后显示不出来
- cocos-js,内存管理3---查看器
- cocos2dx项目在android平台第一次安装的一个奇怪的bug
- 【转载】Cocos2dx 小技巧(十四)ScrollView实现缩放效果
- 【转载】cocos2d-x3.0 关于ScrollView的使用
- Cocos2dx----GL calls GL verts
- QuickCocos使用总结
- cocos2d-x 3.x学习之TouchOneByOne单点触摸
- Windows下新建Cocos2d-x V3.10项目(cocos2d-x lua)