Cocos2d-x3.1 multi-point touch, cocos2d-x3.1 touch

Source: Internet
Author: User

Cocos2d-x3.1 multi-point touch, cocos2d-x3.1 touch

#include "cocos2d.h"#include "ui/CocosGUI.h"USING_NS_CC;using namespace ui;class HelloWorld : public cocos2d::Layer{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();          void onTouchesBegan(const std::vector<Touch*>& touches,cocos2d::Event* event);    void onTouchesMoved(const std::vector<Touch*>& touches,cocos2d::Event* event);    void onTouchesEnded(const std::vector<Touch*>& touches,cocos2d::Event* event);    void onTouchesCancel(const std::vector<Touch*>& touches,cocos2d::Event* event);    // a selector callback    void menuCloseCallback(cocos2d::Ref* pSender);        // implement the "static create()" method manually    CREATE_FUNC(HelloWorld);};


# Include "HelloWorldScene. h "# include" ui/CocosGUI. h "USING_NS_CC; using namespace ui; static const Color3B * s_TouchColors [EventTouch: MAX_TOUCHES] ={& Color3B: YELLOW, & Color3B: BLUE, & Color3B: GREEN, & Color3B: RED, & Color3B: MAGENTA}; class TouchPoint: public Node {public: // default constructor TouchPoint () {setGLProgramState (GLProgramState: getOrCreateWithGLProgramName (GLProgram :: SHADER_NAME_POSITION_TEXTURE_CO LOR);} // draw function virtual void draw (Renderer * renderer, const Mat4 & transform, bool transformUpdated) {// set the line color DrawPrimitives: setDrawColor4B (_ touchColor. r, _ touchColor. g, _ touchColor. b, 255); // set the line width glLineWidth (10); // draw the DrawPrimitives: drawLine (Vec2 (0, _ touchPoint. y), Vec2 (getContentSize (). width, _ touchPoint. y); DrawPrimitives: drawLine (Vec2 (_ touchPoint. x, 0), Vec2 (_ touchPoint. x, getConte NtSize (). height); // set the line width glLineWidth (1); // set the vertex size DrawPrimitives: setPointSize (30); // draw vertex DrawPrimitives: drawPoint (_ touchPoint );} // set the touch point position void setTouchPos (const Vec2 & pt) {_ touchPoint = pt;} // set the touch point color void setTouchColor (Color3B color) {_ touchColor = color ;} // construct the touch point static TouchPoint * touchPointWithParent (Node * pParent) {auto pRet = new TouchPoint (); pRet-> setContentSize (pParent-> getConte NtSize (); pRet-> setAnchorPoint (Vec2: ZERO); pRet-> autorelease (); return pRet;} private: Vec2 _ touchPoint; Color3B _ touchColor ;}; scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "I Nit "you need to initialize your instancebool HelloWorld: init () {// 1. super init first if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Vec2 origin = Director: getInstance () -> getVisibleOrigin (); // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autoreltailobject 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); // create menu, it's an autorelease object auto menu = Menu: create (closeItem, NULL); menu-> setPosition (Vec2 :: ZERO); this-> addChild (menu, 1); // set event listening 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 ); // Add to event distributor ctor: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this); // tag auto title = Label :: createWithSystemFont ("Please touch the screen", "", 24); title-> setPosition (Vec2 (200,200); addChild (title); return true;} static Map <int, touchPoint *> s_map; void HelloWorld: onTouchesBegan (const std: vector <Touch *> & touches, cocos2d: Event * event) {for (auto & item: touches) {auto touch = item; auto touchPoint = TouchPoint: touchPointWithParent (this); // initialize the touch point auto location = touch-> getLocation (); touchPoint-> setTouchPos (location ); // set the touch position touchPoint-> setTouchColor (* s_TouchColors [touch-> getID ()]); // set the color addChild (touchPoint); s_map.insert (touch-> getID (), touchPoint) ;}} void HelloWorld: onTouchesMoved (const std: vector <Touch *> & touches, cocos2d: Event * event) {for (auto & item: touches) {auto touch = item; auto pTP = s_map.at (touch-> getID (); auto location = touch-> getLocation (); pTP-> setTouchPos (location );}} void HelloWorld: onTouchesEnded (const std: vector <Touch *> & touches, cocos2d: Event * event) {for (auto & item: touches) {auto touch = item; auto pTP = s_map.at (touch-> getID (); removeChild (pTP, true); s_map.erase (touch-> getID ();} void HelloWorld :: onTouchesCancel (const std: vector <Touch *> & touches, cocos2d: Event * event) {onTouchesEnded (touches, 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 multi-touch Compilation

We recommend that you modify the project file based on the example to avoid these errors.

What is a multi-point touch screen mobile phone?

Introduction to multi-point touch screen
Unlike the traditional single-point touch screen, multi-point touch screen features two hands, multiple fingers, or even multiple people simultaneously operating the screen content, making it more user-friendly. Multi-touch technology is also called multi-touch technology. Multi-touch is divided into two layers in practical application: one is that the master chip can collect multiple points of signal at the same time, and the other is to judge the significance of each finger touch signal. In other words, it can provide Gesture Recognition for users. Apple's iPhone and MacBook laptops are listed, some products of Motorola and HTC can basically achieve this purpose. However, currently, the iPhone only allows two fingers to work simultaneously for rotation, scaling, and other functions, microsoft's Surface Computer is even more amazing. It can reflect multiple contacts at the same time. Compared with a small number of signal lines of the traditional single-point touch screen 4pin or 5pin, the multi-point touch screen separates many touch units on the conductive layer, and each unit connects to the external circuit through a separate lead. All touch units are displayed in matrix la S. Therefore, no matter which part the user's finger is exposed to, the system can respond to the corresponding finger action.

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.