Move.h
#ifndef __rpg_move__move__
#define __RPG_MOVE__MOVE__
#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
Class Move:public Cclayer
{public
:
static move* createmove (Ccsprite *_play, Cctmxtiledmap *_map);
void Initmove (Ccsprite *_play, Cctmxtiledmap *_map);
ccsprite* play;
cctmxtiledmap* map;
float SW, SH; Device width and height
bool isUp, Isdown, Isleft, isright;//Identifies whether the protagonist moves
bool Mapup, Mapdown, Mapleft, mapright;//identifies if the map is moving
void Update (float time);//update function
void logic ();//logical function
void move ();//The protagonist and map move function
virtual void OnEnter () ;
virtual void OnExit ();
virtual bool Cctouchbegan (Cctouch *ptouch, ccevent *pevent);
virtual void cctouchmoved (Cctouch *ptouch, ccevent *pevent);
virtual void cctouchended (Cctouch *ptouch, ccevent *pevent);
};
#endif/* Defined (__rpg_move__move__) */
Move.cpp
#include "Move.h" move* move::createmove (ccsprite *_play, Cctmxtiledmap *_map) {move* layer = new Move ();
if (Layer && layer->init ()) {layer->autorelease ();
Layer->initmove (_play, _map);
return layer;
} cc_safe_delete (layer);
return NULL;
} void Move::initmove (Ccsprite *_play, Cctmxtiledmap *_map) {play = _play;
Map = _map;
Get equipment and width high SW = Ccdirector::shareddirector ()->getwinsize (). width;
SH = Ccdirector::shareddirector ()->getwinsize (). Height; Scheduleupdate (); Call Update function void update (float time)} void Move::update (float time) {logic ();//Judge logical Move (); Move} void Move::logic () {if (isright) {//In case the map does not exceed the screen if (Map->getpositionx () > SW-MAP-&G
T;getcontentsize (). width) {if (Play->getpositionx () < sw*0.5) {}else{
Mapright = true;
Play->setpositionx (sw*0.5); }}else{Mapright = false; }} if (Isleft) {//In case the map does not exceed the screen if (Map->getpositionx () < 0) {if (PLAY-&G
T;getpositionx () > sw*0.5) {}else{mapleft = true;
Play->setpositionx (sw*0.5);
}}else{Mapleft = false; }} if (isUp) {//In case the map does not exceed the screen if (Map->getpositiony () > Sh-map->getcontentsize (). Height) {if (Play->getpositiony () < sh*0.5) {}else{mapup
= true;
Play->setpositiony (sh*0.5);
}}else{Mapup = false; }} if (Isdown) {//In case the map does not exceed the screen if (Map->getpositiony () < 0) {if (PLAY-&G
T;getpositiony () > sh*0.5) {}else{Mapdown = true; Play->setpositiony (sh*0.5);
}}else{Mapdown = false;
}}} void Move::move () {//Determines whether the protagonist moves if (isUp) {play->setpositiony (Play->getpositiony () +1);
} if (Isdown) {play->setpositiony (Play->getpositiony ()-1);
} if (Isleft) {Play->setpositionx (Play->getpositionx ()-1);
} if (isright) {Play->setpositionx (Play->getpositionx () +1);
}//Determine if the map is moving if (mapup) {map->setpositiony (Map->getpositiony ()-1);
} if (Mapdown) {map->setpositiony (Map->getpositiony () +1);
} if (Mapleft) {Map->setpositionx (Map->getpositionx () +1);
} if (mapright) {Map->setpositionx (Map->getpositionx ()-1); }}//When the user point starts to move bool Move::cctouchbegan (Cctouch *ptouch, ccevent *pevent) {if (Ptouch->getlocation (). x >
sw*0.8) {isright = true; } if (Ptouch->getLocation (). x < sw*0.2) {Isleft = true;
} if (Ptouch->getlocation (). Y > sh*0.8) {isUp = true;
} if (Ptouch->getlocation (). Y < sh*0.2) {Isdown = true;
} return true; } void Move::cctouchmoved (Cctouch *ptouch, Ccevent *pevent) {}//stop moving void move::cctouchended when user lifts (Cctouch *ptouch,
Ccevent *pevent) {isUp = false;
Isdown = false;
Isleft = false;
Isright = false;
Mapup = false;
Mapdown = false;
Mapleft = false;
Mapright = false; } void Move::onenter () {ccdirector::shareddirector ()->gettouchdispatcher ()->addtargeteddelegate (this, 0, True
);
Cclayer::onenter ();
} void Move::onexit () {ccdirector::shareddirector ()->gettouchdispatcher ()->removedelegate (this);
Cclayer::onexit (); }