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