COCOS2DX 3.x (click on the screen to move the sprite, drag the sprite) do not need to write callback function

Source: Internet
Author: User
Tags addchild

1 //2 //MainScene.cpp3 //HelloWorld4 //5 //Created by Apple on 16/9/19.6 //7 //8 9#include"mainscene.hpp"TenScene *Mainscene::createscene () One { AAuto scene =scene::create (); -     //Creating Layers -Mainscene *layer =mainscene::create (); theScene->addChild (layer); -     returnscene; - } - BOOLMainscene::init () { +     if(!Layer::init ()) { -         return false; +     } A      at      -Sprite = Sprite::create ("Hu.png"); -Sprite->setposition ( -, -); -Sprite->setanchorpoint (VEC2 (0.5,0.5)); -      This->addchild (sprite);//Add to Layer -      in    -      to     //Statement +Auto Listener1 =eventlistenertouchonebyone::create (); -Listener1->setswallowtouches (true); the      *     //a method of directly implementing the callback of touch events via lambda expressions $Listener1->ontouchbegan = [] (touch* Touch, event*Event){Panax NotoginsengAuto target = static_cast<sprite*> (Event-getcurrenttarget ()); -          thePoint Locationinnode = Target->converttonodespace (touch->getLocation ()); +Size s = target->getcontentsize (); ARect rect = rect (0,0, S.width, s.height); the          +         if(Rect.containspoint (locationinnode)) -         { $Log"Sprite began ... x =%f, y =%f", locationinnode.x, locationinnode.y); $Target->setopacity ( the);//when clicked, the sprite color is dimmed, 255 is the maximum, and 0 is the smallest . -             return true; -         } the         return false; -     };Wuyi      the      -      Wulistener1->ontouchmoved = [] (touch* Touch, event*Event){ -Auto target = static_cast<sprite*> (Event-getcurrenttarget ()); AboutTarget->setposition (target->getposition () + touch->Getdelta ()); $     }; -      -listener1->ontouchended = [=] (touch* Touch, event*Event){ -Auto target = static_cast<sprite*> (Event-getcurrenttarget ()); ALog"Sprite ontouchesended."); +Target->setopacity (255);//The sprite restores its original color when the gesture is released. the     }; -      $      the     //bind touch Events to sprites the_eventdispatcher->addeventlistenerwithscenegraphpriority (Listener1, sprite); the_eventdispatcher->addeventlistenerwithscenegraphpriority (listener1->Clone (), Sprite); the_eventdispatcher->addeventlistenerwithscenegraphpriority (listener1->Clone (), Sprite); -      in     return true; the}

COCOS2DX 3.x (click on the screen to move the sprite, drag the sprite) do not need to write callback function

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.