[Cocos2d-x 3.0] Touch Monitor

Source: Internet
Author: User

First, basic use

1. First, declare a listener

There are two kinds, eventlistenertouchonebyone and eventlistenertouchallatonce, the former is single touch. The latter is multi-touch. I didn't use the latter much. Not very well understood. So the main point here is single touch.

2. Implement the corresponding callback method for touching and listening various events

There are also two kinds of

1) cc_callback_2 callback mechanism

2) Use lambda expression directly

3, register monitoring

Demo sample

1) using cc_callback_2 callback mechanism

    The statement listens for    auto listener = eventlistenertouchonebyone::create ();        Implement Listener callback    Listener->ontouchbegan = Cc_callback_2 (Test::ontouchbegan, this);    listener->ontouchmoved = Cc_callback_2 (test::ontouchmoved, this);        listener->ontouchended = Cc_callback_2 (test::ontouchended, this);        Register monitor    _eventdispatcher->addeventlistenerwithscenegraphpriority (listener, this);
Three callback methods, no detailed content is written here
BOOL Test::ontouchbegan (Cocos2d::touch *touch, cocos2d::event *event) {    return true;} void Test::ontouchmoved (Cocos2d::touch *touch, cocos2d::event *event) {    }void test::ontouchended (Cocos2d::Touch * Touch, Cocos2d::event *event) {    }
2) using lambda expressions
    The statement listens for    auto listener = eventlistenertouchonebyone::create ();        Implement a listener callback    Listener->ontouchbegan = [] (Touch * touch, Event *event) {                return true;    };    listener->ontouchmoved = [] (Touch * touch, Event *event) {            };        listener->ontouchended = [] (Touch * touch, Event *event) {            };        Register monitor    _eventdispatcher->addeventlistenerwithscenegraphpriority (listener, this);
Assume that it is not a callback method to be used repeatedly. With Lambda is a very good choice, intuitive, can save a lot of things. It's just that lambda can actually be called multiple times. This is not to say.

Second, drag the sprite to move

We're going to do the following steps.

1. Get Touch coordinates

2. Infer if the coordinates are included in the sprite.

3. Get the difference between the movement of the mouse (that is, our finger movement)

4. The distance and direction of the sprite movement

    listener->ontouchmoved = [=] (Touch * touch, Event *event) {                //get touch coordinates----here getlocation () This method will voluntarily convert the coordinates to OpenGL coordinates. We do not need to manually convert        auto Touchpos = Touch->getlocation ();                Infer if in Sprite        if (Sp->getboundingbox (). Containspoint (Touchpos)) {                        //Get difference            auto Movepos = touch-> Getdelta ();                        Elf Mobile            Sp->setposition (sp->getposition () + Movepos);        }    ;
Here I would like to say Getdelta () this method. Once obtained the difference is for our own calculation, through the Getpreviouslocation() This method obtains the last coordinate and then subtracts from today's coordinates. Now there is a way to calculate the difference. It's kind of sweet--.

Oh, that's right. Here the lambda expression needs to add the = Sign in [] to indicate the value passed. This is like a talent for external referencing and modifying variables. About Ben can go to see Lambda to use

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

[Cocos2d-x 3.0] Touch Monitor

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.