9. Cocos2d-x Game Programming Multi-point touch

Source: Internet
Author: User
Tags addchild

The previous article talked about a single touch, and then this article went on to touch more. Multi-touch means that multiple fingers operate on the screen at the same time, then trigger the listener, and the callback method implements the function.


1, first understand how this code debugging on the phone. Because this is a listener that is related to multiple finger touches to trigger, it is not possible to touch the finger on the simulator. Must run on the real machine.

It's really simple, as long as in the project iOS this folder in the appcontroller.mm file inside the -(BOOL) Application: (uiapplication  *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions {} method to fill in a sentence [Eaglview setmultipletouchenabled:YES]; All right, look at the picture in detail:



2, then, we will be on the code, very simple, but in a single touch on the basis of the Ontouchbegan, such as these words into the plural ontouchesbegan and so on, on the English class are understand.

Code (FIRTH.H):

#include "cocos2d.h" Using_ns_cc;class firthtest:public cocos2d::layer{/* touchallatonce */private:    Double _ Distance;   Distance between two touch points    double _deltax;     X-axis Change value    double _deltay;     The change value of the y-axis    Sprite *_bgsprite;  Elf    double _mscale;     Scale public:    static cocos2d::scene* createscene ();        virtual BOOL init ();        /* Touchallatonce *    /void Ontouchesbegan (const std::vector<touch *> &touches, cocos2d::event *event); C14/>void ontouchesmoved (const Std::vector<touch *> &touches, cocos2d::event *event);    void ontouchesended (const std::vector<touch *> &touches, cocos2d::event *event);    void ontouchescancelled (const std::vector<touch *> &touches, cocos2d::event *event);        void Menuclosecallback (cocos2d::ref* psender);        void Scale9 ();        Create_func (firthtest);}; #endif/* Defined (__helloworld__fireth__) */

(Firth.cpp)

USING_NS_CC;    scene* firthtest::createscene () {Auto scene = Scene::create ();    Auto layer = Firthtest::create ();    Scene->addchild (layer); return scene;} BOOL Firthtest::init () {if (!    Layer::init ()) {return false;        }/** * Gets the visible OpenGL view Size */Size size = Director::getinstance ()->getvisiblesize ();        /** * Get the visible OpenGL view starting point (0,0) */Vec2 origin = Director::getinstance ()->getvisibleorigin (); /** * Create menu item */Auto Closeitem = Menuitemimage::create ("Closenormal. PNG "," Closeselected.png ", Cc_callback        _1 (Firthtest::menuclosecallback, this));                                Closeitem->setposition (VEC2 (origin.x + size.width-closeitem->getcontentsize (). WIDTH/2,        ORIGIN.Y + closeitem->getcontentsize (). HEIGHT/2)); /** * Create Menu */Auto Menu = Menu::create (Closeitem, NULL);    Menu->setposition (Vec2::zero);        This->addchild (menu, 1);    /** * Create title */Auto title = Label::createwithsystemfont ("Multi-Touch", "", 50);    Title->setposition (SIZE.WIDTH/2, size.height * 0.9);        This->addchild (title);    /** * Create an elf */_bgsprite = sprite::create ("Helloworld.png");    _bgsprite->setposition (VEC2 (SIZE.WIDTH/2 + origin.x, SIZE.HEIGHT/2 + origin.y));        This->addchild (_bgsprite, 0);        _mscale = 1.0;        /** * Create multi-touch listener */Auto Listener = eventlistenertouchallatonce::create ();    Listener->ontouchesbegan = Cc_callback_2 (Firthtest::ontouchesbegan, this);    listener->ontouchesmoved = Cc_callback_2 (firthtest::ontouchesmoved, this);    listener->ontouchesended = Cc_callback_2 (firthtest::ontouchesmoved, this);        listener->ontouchescancelled = Cc_callback_2 (firthtest::ontouchesmoved, this); /** * Register for monitoring Events */_eventdispatcher->addeventlistenerwithscenegraphpRiority (listener, this); return true;} void Firthtest::ontouchesbegan (const std::vector<touch *> &touches, cocos2d::event *event) {if (touches.size                () >= 2) {//Get first point auto Touch1 = touches.at (0);                VEC2 myPoint1 = Touch1->getlocation ();                Get the second point auto TOUCH2 = touches.at (1);                VEC2 MyPoint2 = Touch2->getlocation ();                         /** * Pythagorean theorem, figure out two points distance */_distance = sqrt ((mypoint2.x-mypoint1.x) * (mypoint2.x-mypoint1.x) +            (mypoint2.x-mypoint1.x) * (MYPOINT2.Y-MYPOINT1.Y)); }}void firthtest::ontouchesmoved (const Std::vector<touch *> &touches, cocos2d::event *event) {if (touches.si                Ze () >= 2) {//Get first point auto Touch1 = touches.at (0);                VEC2 myPoint1 = Touch1->getlocation ();                Get the second point auto TOUCH2 = touches.at (1); VEC2 MyPoint2 = Touch2->getlocation ();                /** * Pythagorean theorem, calculated two points distance * * Double mdistance = sqrt ((mypoint2.x-mypoint1.x) * (MyPoint2        . x-mypoint1.x) + (mypoint2.x-mypoint1.x) * (MYPOINT2.Y-MYPOINT1.Y)); <span class= "S1" style= "font-family:arial, Helvetica, Sans-serif;" >//</span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;" > New Distance </span><span class= "S1" style= "font-family:arial, Helvetica, Sans-serif;" >/</span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;" > Old distance </span><span class= "S1" style= "font-family:arial, Helvetica, Sans-serif;" >*</span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;"                > Original zoom ratio, which is the new zoom ratio </span> _mscale = mdistance/_distance * _mscale;        _distance = mdistance;    Set a new scale _bgsprite->setscale (_mscale); }}void firthtest::ontouchesended (const Std::vector<touch *> &toUches, Cocos2d::event *event) {}void firthtest::ontouchescancelled (const Std::vector<touch *> &touches, Cocos2d::event *event) {}void firthtest::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}


In fact, the code is not difficult, just look at the comments, quite detailed.


Nineth Chapter, End!

This article is original, reproduced please specify the source, prohibited for commercial use. Thank you!

9. Cocos2d-x Game Programming Multi-point touch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.