COCOS2DX 2.2 Touch Event

Source: Internet
Author: User

COCOS2DX 2.2 Touch Event

To enable the Genie to receive touch events, there are three things to do.

Registering touch events;

Receive touch events;

Handles touch events.

Let's start with the three points below to see how the Genie responds to touch events.

1. Registering Touch Events

The Elf class poker inherits Sprites and Cctargetedtouchdelegate, and overrides the Cctargetedtouchdelegate's three functions cctouchbegan,cctouchmoved,cctouchended

Join the Auxiliary Function rect () and Containtouchpoint (cctouch* Touch) for subsequent judgments.

Poker.h file:

[CPP]View Plaincopyprint?
  1. Class Poker:public Ccsprite, public cctargetedtouchdelegate
  2. {
  3. Pokerstate m_state;
  4. Public
  5. Poker (void);
  6. Ccrect rect ();
  7. virtual void onEnter ();
  8. virtual void onExit ();
  9. Virtual ~poker (void);
  10. Boolean containtouchpoint (cctouch* touch);
  11. virtual bool Cctouchbegan (Cctouch *touch, ccevent *event);
  12. virtual void cctouchmoved (Cctouch *touch, ccevent *event);
  13. virtual void cctouchended (Cctouch *touch, ccevent *event);
  14. };

Poker.cpp file:

There is a need to add specific registration behavior in the Poker.cpp, OnEnter and onexit functions are called when sprites are created and destroyed, because you can add registration and destroy registrations in both functions.

[CPP]View Plaincopyprint?
    1. Called when Ccnode enters the scene
    2. void Poker::onenter ()
    3. {
    4. ccdirector* pdirector = Ccdirector::shareddirector ();
    5. Pdirector->gettouchdispatcher ()->addtargeteddelegate (this, 0, true);
    6. Ccsprite::onenter ();
    7. }
    8. Ccnode call when exiting a scene
    9. void Poker::onexit ()
    10. {
    11. ccdirector* pdirector = Ccdirector::shareddirector ();
    12. Pdirector->gettouchdispatcher ()->removedelegate (this);
    13. Ccsprite::onexit ();
    14. }

Above, our touch event registration process is complete.

2. Receive touch event receive touch event, is actually rewrite Cctargetedtouchdelegate's three function cctouchbegan,cctouchmoved,cctouchended. [CPP]View Plaincopyprint?
    1. BOOL Poker::cctouchbegan (Cctouch *touch, ccevent *event)
    2. {
    3. Cclog ("Poker Cctouchbegan");
    4. return false;
    5. }
    6. void Poker::cctouchmoved (Cctouch *touch, ccevent *event)
    7. {
    8. Cclog ("Poker cctouchmoved");
    9. }
    10. void poker::cctouchended (Cctouch *touch, ccevent *event)
    11. {
    12. Cclog ("Poker cctouchended");
    13. }
So, the receiving process has been completed, no surprises, the run can see the print log. 3. Handling Touch Events the first thing to get is the rectangle where the current sprite is。 And that's what Ccrect poker::rect () needs to do. Note that the way you get here is that the genie uses the system's default anchor point, which is the center of the wizard, and if you change the setanchorpoint, you need to change the calculation method. [CPP]View Plaincopyprint?
    1. Ccrect Poker::rect ()
    2. {
    3. Ccsize size = GetTexture ()->getcontentsize ();
    4. Return Ccrectmake (-SIZE.WIDTH/2,-SIZE.HEIGHT/2, Size.width, size.height);
    5. }
If the current sprite is also viewed as a coordinate system, if the sprite is 100 and the width is 100, then the obtained rectangle should be x = -50, y = -50, Width = +, height = 100 Next, turn the point of the touch event into the point of the current sprite's internal coordinate system. (It may not be a good idea, but every node we inherit from Ccnode can be viewed as a coordinate system.) [CPP]View Plaincopyprint?
    1. Boolean poker::containtouchpoint (cctouch* Touch)
    2. {
    3. Return rect (). Containspoint (Converttouchtonodespacear (Touch));
    4. }
Using the Converttouchtonodespacear function inside Ccsprite, you can convert the current touch point to a point in the sprite's internal coordinate system. Finally, judge the touch event and deal with it. [CPP]View Plaincopyprint?
    1. BOOL Poker::cctouchbegan (Cctouch *touch, ccevent *event)
    2. {
    3. int x = Getpositionx ();
    4. int y = Getpositiony ();
    5. if (Containtouchpoint (Touch))
    6. {
    7. SetPosition (CCP (x, y + 30));
    8. }
    9. Cclog ("Poker Cctouchbegan");
    10. return false;
    11. }
If the current touch point is found inside the ccsprite, the current Ccsprite's Y coordinate is moved up by 30 pixels.

COCOS2DX 2.2 Touch Event

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.