In order to realize the airplane game, the person's finger presses the airplane, can drag the plane to move around, here realizes when your finger presses on the picture of the mobile phone, the finger always presses the screen, the airplane will follow you to walk. At the same time, the boundary condition is added to allow the aircraft to move in your field of view, and the effect is exactly the same as the aircraft game on our mobile phone.
Effect:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/64/wKiom1T8QyfjnzlgABqxNLHvPJQ526.gif "style=" float: none; "title=" C1.gif "alt=" Wkiom1t8qyfjnzlgabqxnlhvpjq526.gif "/>
Cocos2d-x Version: 3.4
Engineering Environment: VS30213
first, code writing
/***@ author Lin Bingwen (e-mail: [email protected]) *@ blog http://linbingwen.blog.51cto.com/*@ time 2015.3.8*@ function The main interface of the game */#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);p rivate: bool Whether the isheroplanecontrol;//aircraft is controlled by the X offset of the float mdeltax;//hero plane as it moves with the finger float mdeltay;//the y offset of the hero plane as it moves with the finger sprite *mheroplane;//hero plane}; #endif __gamemain_h__
#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 Baiyun layer this->addchild (Backlayerdown::create ()); //plus a plane mheroplane = sprite::create ("Air1.png"); mheroplane->setposition (VEC2 (visiblesize.width / 2, visiblesize.height / 5)); this->aDdchild (mheroplane, 1, 100); isheroplanecontrol = false; //open touch, Increase touch Listen 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); director::getinstance ()->geteventdispatcher ()- >addeventlistenerwithscenegraphpriority (listen,this); return true;} Bool gamemain::ontouchbegan (COCOs2d::touch *touch, cocos2d::event *unused_event) { point mheropos = mheroplane->getposition (); point Mbeganpos = touch->getlocationinview (); mbeganpos = director:: GetInstance ()->converttogl (mbeganpos); //determine if the current finger is in the area of the hero plane, and calculate the offset when the plane is moving 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; //Calculation 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 to subtract offsets float y = mMovedPos.y - mdeltay; if (x <= Mheroplane->getcontentsize (). width / 2 + origin.x)//x reach left edge of screen x = mheroplane->getcontentsize (). width / 2 + origin.x; else if (x >= visiblesize.width - mheroplane->getcontentsize (). width / 2)//x reach the right edge of the screen x = visibleSize.width - Mheroplane->getcontentsize () .width / 2; if (y <= mheroplane->getcoNtentsize (). height / 2 + origin.y)//y reach the bottom edge of the screen y = mheroplane->getcontentsize () .height / 2 + origin.y; else if (y >= Visiblesize.height - mheroplane->getcontentsize (). height / 2)//x reach screen top border y = visibleSize.height - Mheroplane->getcontentsize (). height/ 2; //Aircraft Follow Finger movement 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&nbsP;*unused_even) { isheroplanecontrol = false;}
I'll say the main function here:
Header file to increase touch events:
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);
Implement file-on-touch event monitoring:
Open touch, Increase touch Listen 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); Director::getinstance ()->geteventdispatcher ()->addeventlistenerwithscenegraphpriority (listen,this);
Then there is the handling of 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 (mbeganpos); // Determines whether the current finger presses the area of the hero plane, and calculates the offset of the aircraft to be moved 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) {&NBSP;&NBSP;&Nbsp; isheroplanecontrol = true; //Calculate 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 to subtract the offset float y = mMovedPos.y - mDeltaY; if (X <= mheroplane->getcontentsize () .width / 2 + origin.x)//x reach the left edge of the screen x = Mheroplane->getcontentsize () .width / 2 + origin.x; else if (X >= visiblesize.width - mheroplane->getcontentsize () . width / 2)//x reach the right edge of the screen x = visiblesize.width - mheroplane->getcontentsize () .width / 2; if&nbSP; (Y <= mheroplane->getcontentsize (). height / 2 + origin.y)//y reach the bottom edge of the screen y = mHeroPlane-> Getcontentsize () .height / 2 + origin.y; else if (Y >= visiblesize.height - mheroplane->getcontentsize (). Height &NBSP;/&NBSP;2)//x reach the on-screen border y = visiblesize.height - mheroplane->getcontentsize () .height/ 2; The //aircraft follows the finger to move the 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, the code is very small, there is a need to take the above himself, the picture to change, change the name of the class can be.
In fact, this should be a hero and mobile events to write a separate class, and then in the Gamemain to call, because the hero of this Class I also conceived, so first write, the hero will be the plane to separate out to become a class, will not be written in gamemain so much;
Effect:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/64/wKiom1T8QyfjnzlgABqxNLHvPJQ526.gif "style=" float: none; "title=" C1.gif "alt=" Wkiom1t8qyfjnzlgabqxnlhvpjq526.gif "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5A/60/wKioL1T8REXTT8-WAA275rBxn4o876.gif "style=" float: none; "title=" C2.gif "alt=" Wkiol1t8rextt8-waa275rbxn4o876.gif "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5A/64/wKiom1T8Qy3jgfhpABuBZi5igXk095.gif "style=" float: none; "title=" C3.gif "alt=" Wkiom1t8qy3jgfhpabubzi5igxk095.gif "/>
The effect is good, the plane can follow the movement and will not run out of the screen range
second, the idea explanation
1, first in the Ontouchbegan to determine whether the touch point in the hero plane picture rectangle, if within this range, just set the Boolean Mheroplanecontrol to True, and calculate the touch point of the horizontal ordinate and the Hero aircraft Anchor Point coordinates of the difference.
2, because to allow the hero plane to move, to modify the anchor position, you must know the location of the anchor point and the offset of the touch position, before you can set the position of the protagonist through this offset.
3, judge whether the hero plane ran out of the screen range, if it is, set it at the border, look at the above two if judgment.
4. In ontouchmoved, if Mheroplanecontrol is true, the hero plane can be moved.
If you think this article is useful to you, then help me to praise ~ ~ Thank You
This article is from the "Lin Bingwen blog Space" blog, be sure to keep this source http://linbingwen.blog.51cto.com/9912186/1618414
Cocos2d-x "Thunder War"-The elf moves with the finger, you point where I go!