Collision detection of a character
a two-dimensional array is used to record whether each row is already occupied, 0 It's not occupied yet. The following is the complete code:
GameLayer.h
Gamelayer.h#ifndef _game_layer_h_#define _game_layer_h_#include "Cocos2d.h" class level; class Rolesprite; class Gamelayer:public cocos2d::layer{public:static cocos2d::scene* createscene (); Create_func (Gamelayer);p ublic:gamelayer (); ~gamelayer (); virtual bool init ();//@biref Initialize map void Initmap ();//@biref Update M_mapstatevoid updatemapdata (); void Resetmapdata (); virtual bool Ontouchbegan (Cocos2d::touch *touch, cocos2d::event *unused_event); virtual void ontouchmoved (Cocos2d::touch *touch, cocos2d::event *unused_event); virtual void ontouchended (Cocos2d::touch *touch, cocos2d::event *unused_event);//@biref can be moved, parameters: the direction to move, whether to move in the direction of the addition or to reduce the direction of the move bool Iscanmove (bool Ismovehor,bool moveadd);p Rivate:level * m_plevel;//< level data Reference cocos2d::vector<rolesprite*> m_ Prolespritevec;//< Storage Role Wizard COCOS2D::VEC2 m_touchbegin;//< start click location cocos2d::vec2 m_touchmove;//< Move the point in the process of float m _mindis;//< the minimum distance to move, >= him when to determine whether to move Rolesprite * M_pselenode;//< the selected role int m_mapstate[5][4];//<5 row 4 column, 0 is empty, 1 is already populated}; #enDif
GameLayer.cpp
#include "GameLayer.h" #include "RoleSprite.h" #include "data/role.h" #include "data/level.h" USING_NS_CC; scene* gamelayer::createscene () {Auto scene = Scene::create (); Auto layer = Gamelayer::create (); Scene->addchild ( layer); return scene;} Gamelayer::gamelayer (): M_plevel (nullptr), M_pselenode (nullptr), M_touchbegin (VEC2 (0,0)), M_touchmove (VEC2 (0,0)), M _mindis (0) {m_plevel = level::s_levelvec.at (0); Cc_safe_retain (m_plevel); M_mindis = 20.0f;} Gamelayer::~gamelayer () {cc_safe_release_null (m_plevel);} BOOL Gamelayer::init () {Size size = Director::getinstance ()->getwinsize (); Auto Bgsprite = Sprite::create ("Main_ Bg0.png "); Bgsprite->setposition (SIZE.WIDTH/2,SIZE.HEIGHT/2); AddChild (bgsprite); Initmap (); Auto Lisener = Eventlistenertouchonebyone::create (); Lisener->ontouchbegan = Cc_callback_2 (gamelayer::ontouchbegan,this); lisener->ontouchmoved = Cc_callback_2 (gamelayer::ontouchmoved,this); lisener->ontouchended = CC_CALLBACK_2 ( Gamelayer::ontouchended,this);_eventdispatcher->Addeventlistenerwithscenegraphpriority (lisener,this); return true;} void Gamelayer::initmap () {Size size = Director::getinstance ()->getwinsize (); float x = 0; float y = 0; float paddingleft = 10.0f; float paddingbottom = 53.0f; for (unsigned i = 0; i < M_plevel->getroleid (). Size (); i++) {Role * PRole = Role::s_rolevec.at (M_plevel->getroleid (). at (i)); Sprite * pp = sprite::create (Prole->getimagename ()->getcstring ()); int col = M_plevel->getrolepos (). at (i). x; int row = M_plevel->getrolepos (). at (i). y; x = paddingleft + col * 1.0f * 75.0f; y = paddingbottom + row * 1.0f * 75.0f ; Rolesprite * prolesprite = Rolesprite::create (Prole,row,col,rect (x,y,pp->getcontentsize () .width,pp-> Getcontentsize (). Height), this,this->getlocalzorder ()); M_prolespritevec.pushback (prolesprite);} Updatemapdata ();} BOOL Gamelayer::ontouchbegan (Touch *touch, Event *unused_event) {Auto loction = This->converttonodespace (touch-> GetLocation ()); for (unsigned int i = 0; i < M_prolEspritevec.size (); i++) {Rolesprite * prolesprite = m_prolespritevec.at (i); if (Prolesprite->getboundingbox (). Containspoint (loction)) {m_touchbegin = loction; m_pselenode = Prolesprite; break;}} return true;} void Gamelayer::ontouchmoved (Touch *touch, Event *unused_event) {if (M_pselenode && m_pselenode-> Getnumberofrunningactions () > 0) | | !m_pselenode) {return;} Auto loction = This->converttonodespace (Touch->getlocation ()); m_touchmove = loction; float dis = m_ Touchmove.getdistance (m_touchbegin); auto var = m_touchmove-m_touchbegin; Auto Movebypos = VEC2 (0,0); auto Varcolrow = V EC2 (0,0); if (dis >= m_mindis)//move {bool Ismovehor = ABS (var.x) > abs (VAR.Y); bool ismovetop; bool ismoveright; if ( Ismovehor)//Horizontal Movement {ismoveright = var.x > 0; if (var.x > 0)//right{movebypos = Vec2 (75.0f,0); varcolrow = VEC2 (1,0); }else//left{movebypos = VEC2 ( -75.0f,0); varcolrow = VEC2 ( -1,0);}} else//move in vertical direction {ismovetop = var.y > 0; if (var.y > 0)//top{movebypos = VEC2 (0,75.0f); Varcolrow = VEC2 (0,1);} Else//bottom{movebypos = VEC2 (0,-75.0f); varcolrow = VEC2 (0,-1);}} if (Iscanmove (ismovehor,ismovehor? ismoveright:ismovetop)) {M_pselenode->setcol (M_pselenode->getcol () + (int) varcolrow.x); M_pselenode->setrow (M_pselenode->getrow () + (int) varcolrow.y); M_pselenode->runaction ( Moveby::create (0.5f,movebypos)); Updatemapdata ();}}} void gamelayer::ontouchended (Touch *touch, Event *unused_event) {m_touchbegin = VEC2 (0,0); m_touchmove = VEC2 (0,0); m_ Pselenode = nullptr;} BOOL Gamelayer::iscanmove (bool Ismovehor,bool moveadd) {bool Ismove = false; Ccassert (M_pselenode, "Select node should not be null!!"); int width = m_pselenode->getwidth (); int height = m_pselenode->getheight (); int row = M_pselenode->getrow (); int col = M_pselenode->getcol (), if (ismovehor)//Horizontal direction {if (moveadd) {int coltem = col + width;int rowtem = row; int Fillednu m = 0; for (int i = 0; i < height; i++) {Rowtem + = i;if (M_mapstate[rowtem][coltem] = = 1) {fillednum++;} }ismove = (Fillednum = = 0)? True:false;if (m_pselenode->getcol () = = 3) {Ismove = false;}} Else{int Coltem = Col-1;int Rowtem = row, int fillednum = 0, for (int i = 0; i < height; i++) {Rowtem + = I;if (m_maps Tate[rowtem][coltem] = = 1) {fillednum++;}} Ismove = (Fillednum = = 0)? True:false;if (m_pselenode->getcol () = = 0) {Ismove = false;}}} else//Vertical Direction {if (moveadd) {int coltem = col; int rowtem = row + height; int fillednum = 0; for (int i = 0; i < width; i++) {Coltem + = i;if (M_mapstate[rowtem][coltem] = = 1) {fillednum++;}} Ismove = (Fillednum = = 0)? True:false;if (m_pselenode->getrow () = = 4) {ismove = false;}} Else{int coltem = col; int rowtem = row-1; int fillednum = 0; for (int i = 0; i < width; i++) {Coltem + = I;if (m_mapst Ate[rowtem][coltem] = = 1) {fillednum++;}} Ismove = (Fillednum = = 0)? True:false;if (m_pselenode->getrow () = = 0) {Ismove = false;} if (M_pselenode->getrole ()->gettype () = = Kroletypeboss && m_pselenode->getcol () = = 0)//{//ismove = true;//Only the devil can go from here//}}} return ismove;} void Gamelayer::updatemapdata () {resetmapdata (); if (m_prolespritevec.size () = =)//< requires 10 characters 1boss,5 general, 4 soldiers {for ( unsigned int i = 0; i < m_prolespritevec.size (); i++) {Rolesprite * prolesprite = m_prolespritevec.at (i); int width = PR Olesprite->getwidth (); int heigit = Prolesprite->getheight (); int row = Prolesprite->getrow (); int col = PROLESPR Ite->getcol (); for (int i = 0, i < heigit; i++) {for (int j = 0; J < width; j + +) {M_mapstate[row + I][col + j] = 1;}}} /*cclog ("================test=========================="); for (int i = 0; i < 5; i++) {const char *PPP = ""; for (int j = 0; J < 4; J + +) {PPP = Ccstring::createwithformat ("%s%d,", Ppp,m_mapstate[i][j])->getcstring (); Cclog (PPP);} */}void Gamelayer::resetmapdata () {for (int row = 0, row < 5; row++) {for (int col = 0; col < 4; col++) {m_mapstate[ Row][col] = 0; }}}
Source: http://download.csdn.net/detail/c_boy_lu/8594823
Huarong 06--Screen Click event and clash between characters