Cocos2d-x 3.2 mobile game backgrounds and Genie, cocos2d-x game backgrounds
1. Add a listener first.
auto listener = EventListenerTouchOneByOne::create();listener->setSwallowTouches(true);listener->onTouchBegan = CC_CALLBACK_2(StartGame::onTouchBegan, this);listener->onTouchMoved = CC_CALLBACK_2(StartGame::onTouchMoved, this);listener->onTouchEnded = CC_CALLBACK_2(StartGame::onTouchEnded, this);_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2. 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-> getpreviuslocation (); oldTouchLocation = this-> convertToNodeSpace (oldTouchLocation); Point translation = touchLocation-oldTouchLocation; // get the moving 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); // The background movement function.
// This-> panForTranslation (translation); // genie mobile function} void StartGame: onTouchEnded (Touch * touch, Event * event ){}
3. method implementation
This is a background event. Load a background image in init.
Void StartGame: boundLayerPos (Point newtouch) {Size winSize = Director: getInstance ()-> getWinSize (); Point newPos = this-> getPosition () + newtouch; auto map = getChildByTag (102); // obtain the background image 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 genie mobile Method
Load an genie in init
Void StartGame: panForTranslation (Point translation) {auto sprite = getChildByTag (101); // get the genie Point newPos = sprite-> getPosition () + translation; sprite-> setPosition (newPos );}
This is OK.
The sprite or background image movement obtains the original Touch point and the moving touch point in the onTouchMoved (Touch * touch, Event * event) of the Touch screen Event to get their difference, relocates setPosition (Point * point) for the genie or background image to complete the movement of the background and the genie (the background image must determine the boundary value)
Note that Node can return the boundingbox of the genie through the getBoundingBox function. This is much better than manually calculating the sprite boundary rectangle. Because, first, it is simpler; second, it considers the Location Coordinate Transformation of the genie. (For example, if the anchor point changes, you need to execute the corresponding matrix transformation. For more information, see the source code .)
When you click the selected option, you can first let the genie do some operations. Here is the 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); } }