#ifndef __STATE_H__#define __STATE_H__class Monkey;class State{public: virtual voID execute(Monkey *mk) = 0;};#endif
#ifndef __StopSTATE_H__#define __StopSTATE_H__#include "State.h"#include "Monkey.h"#include "TrunState.h"class StopState : public State{public: virtual voID execute(Monkey *mk);};#endif
#include "StopState.h"voID StopState::execute(Monkey *mk){ if (mk->isstopTimeOut()) { mk->changeState(new TrunState()); mk->walk(); }}
#ifndef __TRUNSTATE_H__#define __TRUNSTATE_H__#include "State.h"#include "Monkey.h"#include "WalkState.h"class TrunState : public State{ virtual voID execute(Monkey *mk);};#endif
#include "TrunState.h"voID TrunState::execute(Monkey *mk){ mk->changeState(new WalkState()); mk->walk();}
#ifndef __WALKSTATE_H__#define __WALKSTATE_H__#include "State.h"#include "Monkey.h"#include "StopState.h"#include "TrunState.h"class WalkState : public State{ virtual voID execute(Monkey *mk);};#endif
#include "WalkState.h"voID WalkState::execute(Monkey *mk){ mk->walk(); if (mk->isWalkOutborder()) { mk->changeState(new TrunState()); mk->trun(); } else if (mk->isWalkTimeOut()) { mk->changeState(new StopState()); mk->stop(); }}
#ifndef __MONKEY_H__#define __MONKEY_H__#include "cocos2d.h"#include <time.h>#include "State.h"#include "StopState.h"#include "WalkState.h"#include "TrunState.h"using namespace std;using namespace cocos2d;#define MAX_Stop_TIME 10#define MAX_WALK_TIME 20#define MAX_WALK_disT 100 enum MonkeyState{ stStop,stWalk,stTrun,};class Monkey : public Node{public: Monkey(); CREATE_FUNC(Monkey); //overrIDe virtual bool init(); virtual voID update(float delta); voID changeState(State *newState); voID stop(); voID walk(); voID trun(); bool isstopTimeOut(); bool isWalkTimeOut(); bool isWalkOutborder();private: Sprite* sp; State *_curState; time_t _curTime; int _curPos; int _curStep;};#endif
#include "Monkey.h"Monkey::Monkey(){}bool Monkey::init(){ if (!Node::init()) { return false; } this->_curPos = 0; this->_curStep = 1; this->_curState = NulL; this->changeState(new WalkState()); this->scheduleUpdate(); this->sp = Sprite::create("eraser.png"); sp:set@R_301_4612@(Vec2(0,0)); this->addChild(this->sp); return true;}voID Monkey::update(float delta){ if (this->_curState) { this->_curState->execute(this); }}voID Monkey::changeState(State *newState){ State *oldState = this->_curState; this->_curState = newState; if (oldState) { delete oldState; } this->_curTime = time(0);}voID Monkey::stop(){ log("stop()");}voID Monkey::walk(){ this->_curPos += this->_curStep; this->sp->set@R_301_4612@(Vec2(this->_curPos*0.5,0)); log("walk(): pos=%d",this->_curPos);}voID Monkey::trun(){ this->_curStep *= -1; log("turn(): step=%d",this->_curStep);}bool Monkey::isstopTimeOut(){ return (time(0) - this->_curTime > MAX_Stop_TIME);}bool Monkey::isWalkTimeOut(){ return (time(0) - this->_curTime > MAX_WALK_TIME);}bool Monkey::isWalkOutborder(){ return (this->_curPos > MAX_WALK_disT || this->_curPos < -MAX_WALK_disT);}总结
以上是内存溢出为你收集整理的Cocos2dx_有限状态机_2全部内容,希望文章能够帮你解决Cocos2dx_有限状态机_2所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)