Cocos2d-x lightning war-the genie move with your fingers, where do you want me to go !, Cocos2d sprite Frame
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka
In this article, we want to implement airplane games where people can drag their fingers around the plane. Here, when you press your fingers on the pictures of your mobile phone, your fingers keep pressing the screen, the plane will follow you. Meanwhile, boundary judgment conditions are added to move the plane in your field of view, achieving the same effect as airplane games on our mobile phones.
Effect:
Cocos2d-x version: 3.4
Project Environment: VS30213
I. Coding
1. header file GameMain. h
/*** @ Author Lin bingwen (Email: ling20081005@126.com) * @ blog http://blog.csdn.net/evankaka#@2015.3.8 * @ function game main interface */# ifndef _ GameMain_H __# define _ GameMain_H __# include "BackLayerDown. h "# include" BackLayerUp. h "# include" cocos2d. h "USING_NS_CC; class GameMain: public cocos2d: Layer {public: static cocos2d: Scene * createScene (); virtual bool init (); virtual bool onTouchBegan (cocos2d :: touch * touch, cocos2d: Event * unused_event); virtual void onTouchMoved (cocos2d: Touch * touch, cocos2d: Event * unused_event); virtual void onTouchEened (cocos2d :: touch * touch, cocos2d: Event * unused_event); virtual void onTouchCancelled (cocos2d: Touch * touch, cocos2d: Event * unused_even); CREATE_FUNC (GameMain); private: bool isHeroPlaneControl; // whether the aircraft is controlled by float mDeltaX; // The X offset float mDeltaY of the hero aircraft when its fingers move; // The Y offset Sprite * mHeroPlane when the hero Plane moves with his finger; // hero plane}; # endif // _ GameMain_H __
Then add the following in GameMain. cpp:
# Include "GameMain. h "USING_NS_CC; Scene * GameMain: createScene () {auto scene = Scene: create (); auto layer = GameMain: create (); scene-> addChild (layer); return scene;} bool GameMain: init () {Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director:: getInstance ()-> getVisibleOrigin (); // this is the ground layer this-> addChild (BackLayerUp: create (); // this is the white cloud layer this-> addChild (BackLayerDown:: create (); // Add an aircraft mHeroPlane = Sprite: create ("air1.png"); mHeroPlane-> setPosition (Vec2 (visibleSize. width/2, visibleSize. height/5); this-> addChild (mHeroPlane, 1,100); isHeroPlaneControl = false; // open the touch and add the touch listening event this-> setTouchEnabled (true ); auto listen = listener: create (); listen-> onTouchBegan = CC_CALLBACK_2 (GameMain: onTouchBegan, this); listen-> onTouchMoved = CC_CALLBACK_2 (GameMain: callback, this ); listen-> onTouchEnded = CC_CALLBACK_2 (GameMain: onTouchEened, this); listen-> onTouchCancelled = CC_CALLBACK_2 (GameMain: onTouchCancelled, this); listen-> false ); // do not intercept the touch Event Director: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listen, this); return true;} bool GameMain: onTouchBegan :: touch * touch, cocos2d: Event * unused_event) {Point mHeroPos = mHeroPlane-> getPosition (); Point mBeganPos = touch-> getLocationInView (); mBeganPos = Director: getInstance () -> convertToGL (mBeganPos); // determines whether the current finger pressed area is the area of the hero aircraft and calculates the offset if (mBeganPos) when the aircraft is to be moved. x> mHeroPos. x-mHeroPlane-> getContentSize (). width/2 & mBeganPos. x <mHeroPos. x + mHeroPlane-> getContentSize (). width/2 & mBeganPos. y> mHeroPos. y-mHeroPlane-> getContentSize (). height/2 & mBeganPos. y <mHeroPos. y + mHeroPlane-> getContentSize (). height/2) {isHeroPlaneControl = true; // calculates the offset mDeltaX = mBeganPos. x-mHeroPos. x; mDeltaY = mBeganPos. y-mHeroPos. y;} return true;} void GameMain: onTouchMoved (cocos2d: Touch * touch, cocos2d: Event * unused_event) {if (isHeroPlaneControl) {Point mMovedPos = touch-> getLocationInView (); mMovedPos = Director: getInstance ()-> convertToGL (mMovedPos); Size visibleSize = Director: getInstance () -> getVisibleSize (); Point origin = Director: getInstance ()-> getVisibleOrigin (); float x = mMovedPos. x-mDeltaX; // remember the offset float y = mMovedPos. y-mDeltaY; if (x <= mHeroPlane-> getContentSize (). width/2 + origin. x) // x to the left boundary of the screen x = mHeroPlane-> getContentSize (). width/2 + origin. x; else if (x> = visibleSize. width-mHeroPlane-> getContentSize (). width/2) // x to the right of the screen x = visibleSize. width-mHeroPlane-> getContentSize (). width/2; if (y <= mHeroPlane-> getContentSize (). height/2 + origin. y) // y reach the bottom boundary of the screen y = mHeroPlane-> getContentSize (). height/2 + origin. y; else if (y> = visibleSize. height-mHeroPlane-> getContentSize (). height/2) // x to reach the screen boundary y = visibleSize. height-mHeroPlane-> getContentSize (). height/2; // The plane follows the finger to move mHeroPlane-> setPosition (Vec2 (x, y);} void GameMain: onTouchEened (cocos2d: Touch * touch, cocos2d:: Event * unused_event) {isHeroPlaneControl = false;} void GameMain: onTouchCancelled (cocos2d: Touch * touch, cocos2d: Event * unused_even) {isHeroPlaneControl = false ;}
Here, I will write the main functions:
Add a touch event to the header file:
virtual bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event);virtual void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *unused_event);virtual void onTouchEened(cocos2d::Touch *touch, cocos2d::Event *unused_event);virtual void onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *unused_even);
Enable touch event listening for files:
// Open the touch and add the touch listening event this-> setTouchEnabled (true); auto listen = EventListenerTouchOneByOne: create (); listen-> onTouchBegan = CC_CALLBACK_2 (GameMain: onTouchBegan, this); listen-> onTouchMoved = CC_CALLBACK_2 (GameMain: onTouchMoved, this); listen-> onTouchEnded = CC_CALLBACK_2 (GameMain: onTouchEened, this ); listen-> onTouchCancelled = CC_CALLBACK_2 (GameMain: onTouchCancelled, this); listen-> setSwallowTouches (false); // The ctor: getInstance ()-> getEventDispatcher () -> addEventListenerWithSceneGraphPriority (listen, this );
Then we processed the touch event:
Bool GameMain: onTouchBegan (cocos2d: Touch * touch, cocos2d: Event * unused_event) {Point mHeroPos = mHeroPlane-> getPosition (); point mBeganPos = touch-> getLocationInView (); mBeganPos = Director: getInstance ()-> convertToGL, and calculate the offset if (mBeganPos. x> mHeroPos. x-mHeroPlane-> getContentSize (). width/2 & mBeganPos. x <mHeroPos. x + mHeroPlane-> getContentSize (). width/2 & mBeganPos. y> mHeroPos. y-mHeroPlane-> getContentSize (). height/2 & mBeganPos. y <mHeroPos. y + mHeroPlane-> getContentSize (). height/2) {isHeroPlaneControl = true; // calculates the offset mDeltaX = mBeganPos. x-mHeroPos. x; mDeltaY = mBeganPos. y-mHeroPos. y;} return true;} void GameMain: onTouchMoved (cocos2d: Touch * touch, cocos2d: Event * unused_event) {if (isHeroPlaneControl) {Point mMovedPos = touch-> getLocationInView (); mMovedPos = Director: getInstance ()-> convertToGL (mMovedPos); Size visibleSize = Director: getInstance () -> getVisibleSize (); Point origin = Director: getInstance ()-> getVisibleOrigin (); float x = mMovedPos. x-mDeltaX; // remember the offset float y = mMovedPos. y-mDeltaY; if (x <= mHeroPlane-> getContentSize (). width/2 + origin. x) // x to the left boundary of the screen x = mHeroPlane-> getContentSize (). width/2 + origin. x; else if (x> = visibleSize. width-mHeroPlane-> getContentSize (). width/2) // x to the right of the screen x = visibleSize. width-mHeroPlane-> getContentSize (). width/2; if (y <= mHeroPlane-> getContentSize (). height/2 + origin. y) // y reach the bottom boundary of the screen y = mHeroPlane-> getContentSize (). height/2 + origin. y; else if (y> = visibleSize. height-mHeroPlane-> getContentSize (). height/2) // x to reach the screen boundary y = visibleSize. height-mHeroPlane-> getContentSize (). height/2; // The plane follows the finger to move mHeroPlane-> setPosition (Vec2 (x, y);} void GameMain: onTouchEened (cocos2d: Touch * touch, cocos2d:: Event * unused_event) {isHeroPlaneControl = false;} void GameMain: onTouchCancelled (cocos2d: Touch * touch, cocos2d: Event * unused_even) {isHeroPlaneControl = false ;}
The method is very simple, and the amount of code is very small. If you need to take the above, you can change the image and change the class name.
In fact, here we should write a class for the hero and the mobile event separately and then call it in GameMain, because I still have a good idea for the Hero class, so I should write it like this first, later, the hero plane will be extracted separately into a class, so it won't write so much in GameMain;
Effect:
\
The effect is good. The plane can follow and move without going out of the screen.
Ii. Ideas
1. First, determine whether the touch point is in the image rectangle of the hero plane in onTouchBegan. If the touch point is within this range, set the Boolean mHeroPlaneControl to true, the differences between the horizontal and vertical coordinates of the touch point and the anchor coordinate of the hero aircraft are also calculated.
2. To let the hero plane move, you must know the offset between the anchor position and the touch position to modify the anchor position. Then, you can set the main character position through this offset.
3. Determine whether the hero plane is out of the screen range. if yes, set it at the boundary. For details, see the two if judgments above.
4. In onTouchMoved, if mHeroPlaneControl is set to true, the hero plane can be moved.
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka
If you think this article is useful to you, help me with it ~~ Thank you!