Cocos2d-x "Thunder War"-The elf moves with the finger, you point where I go!

Source: Internet
Author: User

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

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:

Cocos2d-x Version: 3.4

Engineering Environment: VS30213


first, code writing

1. header File GameMain.h

/***@ author Lin Bingwen (email: [Email protected]) *@ blog http://blog.csdn.net/evankaka*@ time 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);p Rivate:bool isheroplanecontrol;//Whether the aircraft is controlled by the float mdeltax;//The x offset of the 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 Aircraft}; #endif//__gamemain_h__


then add in the 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 Baiyun layer This->addchild (Backlayerdown::create ());//Add 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 listener 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);//do not intercept touch events 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);// Determines whether the current finger presses the area of the hero plane and calculates the offset if the aircraft is 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) {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) {pointMmovedpos = 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 offset float y = mmovedpo S.y-mdeltay;if (x <= mheroplane->getcontentsize (). WIDTH/2 + origin.x)//x reach left edge of screen x = Mheroplane->getcontentsiz E (). WIDTH/2 + Origin.x;else if (x >= visiblesize.width-mheroplane->getcontentsize (). Width/2)//x reach the right edge of the screen x = Visi Blesize.width-mheroplane->getcontentsize (). Width/2;if (y <= mheroplane->getcontentsize (). HEIGHT/2 + Origin . Y)//y reach the bottom of the screen y = Mheroplane->getcontentsize (). HEIGHT/2 + origin.y;else if (y >= VISIBLESIZE.HEIGHT-MHEROPLANE-&G T;getcontentsize (). HEIGHT/2)//x reach the screen on the Border y = Visiblesize.height-mheroplane->getcontentsize (). height/2;// The airplane 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;}

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::o Ntouchmoved, this): listen->ontouchended = Cc_callback_2 (gamemain::ontoucheened, this);listen-> ontouchcancelled = Cc_callback_2 (gamemain::ontouchcancelled, this); Listen->setswallowtouches (false);// Do not intercept Touch event 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 if the aircraft is 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) {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 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 = visible Size.width-mheroplane->getcontentsize (). Width/2;if (y <= mheroplane->getcontentsize (). HEIGHT/2 + origin.y) Y reaches the bottom edge of the screen y = Mheroplane->getcontentsize (). HEIGHT/2 + origin.y;else if (y >= visiblesize.height-mheroplane->g Etcontentsize (). HEIGHT/2)//x reach the screen on the Border y = Visiblesize.height-mheroplane->getcontentsize (). height/2;// The airplane 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, 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:


\

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.

Lin Bingwen Evankaka original works. Reprint please specify the source Http://blog.csdn.net/evankaka

If you think this article is useful to you, then help me to the top ~ ~ Thank you!

Cocos2d-x "Thunder War"-The elf moves with the finger, you point where I go!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.