1, first new listener event
Auto listener = Eventlistenertouchonebyone::create (); Listener->setswallowtouches (True);listener-> Ontouchbegan = Cc_callback_2 (Startgame::ontouchbegan, this); listener->ontouchmoved = Cc_callback_2 (StartGame::o Ntouchmoved, this): listener->ontouchended = Cc_callback_2 (startgame::ontouchended, this);_eventdispatcher-> Addeventlistenerwithscenegraphpriority (listener, this);
2, the Listener method callback function implementation
bool Startgame::ontouchbegan (touch* touch, event* Event) {Point touchlocation = This->converttouchtonodespace (Touch); This->selectspritefortouch (touchlocation); return true;} void startgame::ontouchmoved (touch* touch, event* Event) {Point touchlocation = This->converttouchtonodespace (Touch ); Point oldtouchlocation = Touch->getpreviouslocation (); Oldtouchlocation = This->converttonodespace (oldtouchlocation); Point translation = touchlocation-oldtouchlocation;//Gets the moved distance Cclog ("Touchlocation--------------------%d-------%d", TOUCHLOCATION.X,TOUCHLOCATION.Y); Cclog ("Oldtouchlocation--------------------%d-------%d", OLDTOUCHLOCATION.X,OLDTOUCHLOCATION.Y); Cclog ("Translation--------------------%d-------%d", TRANSLATION.X,TRANSLATION.Y); This->boundlayerpos (translation);//Background move function
This->panfortranslation (translation);//elf Move function}void startgame::ontouchended (touch* Touch, event* Event) {}
3, method implementation
Background event, loading a background image in Init
void Startgame::boundlayerpos (Point newtouch) { Size winsize = director::getinstance ()->getwinsize (); Point newpos = this->getposition () + Newtouch;auto map = Getchildbytag (102); Get background picture here newpos.x = MIN (newpos.x, 0); Newpos.x = MAX (newpos.x,-map->getcontentsize (). width+winsize.width); Newpos.y = MIN (newpos.y,0); newpos.y = MAX (Newpos.y,-map->getcontentsize (). Height + winsize.height); This->setposition (Newpos);}
Implementation of the Sprite Move method
Load an elf in init
void Startgame::p anfortranslation (point translation) {Auto Sprite = Getchildbytag (101);//Get elf point Newpos = Sprite->getposition () + translation; Sprite->setposition (Newpos); }
This is OK.
The sprite or background image movement is in the touch screen monitoring event ontouchmoved (touch* touch,event *) to get the original touch point and move the touch screen point, to get their difference, to the sprite or background image repositioning SetPosition ( Point * point), which completes the movement of the background and sprite (background image to determine the boundary is worth)
Note that node can return the bounding rectangle of the sprite through the function Getboundingbox. This is much better than manually calculating the bounding rectangle of the sprite yourself. Because, first, it's simpler; second, it takes into account the position-coordinate transformation of the sprite. (such as the anchor point has changed, to perform the corresponding matrix transformation, specific reference to the source code, these are no longer detailed.) )
Click on the selected time can let the elves do some action, here is flashing
void Startgame::selectspritefortouch (Point touchlocation) {Auto Sprite = Getchildbytag (101); if (Sprite->getboundingbox (). Containspoint (touchlocation)) { auto blink = ccblink::create (2.0f,5); Sprite->runaction (blink); } }
Cocos2d-x 3.2 Mobile game backgrounds and Sprites