#ifndef __MONKEY_H__#define __MONKEY_H__#include "cocos2d.h"#include <time.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(MonkeyState newState); voID stop(); voID walk(); voID trun(); bool isstopTimeOut(); bool isWalkTimeOut(); bool isWalkOutborder();private: Sprite* sp; MonkeyState _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(MonkeyState::stStop); this->scheduleUpdate(); this->sp = Sprite::create("eraser.png"); sp:setposition(Vec2(0,0)); this->addChild(this->sp); return true;}voID Monkey::update(float delta){ switch(this->_curState) { case stStop: if (this->isstopTimeOut()) { this->changeState(MonkeyState::stWalk); this->walk(); } break; case stWalk: this->walk(); if (this->isWalkOutborder()) { this->changeState(MonkeyState::stTrun); this->trun(); } else if (this->isWalkTimeOut()) { this->changeState(MonkeyState::stStop); this->stop(); } break; case stTrun: this->changeState(MonkeyState::stWalk); this->walk(); break; }}voID Monkey::changeState(MonkeyState newState){ this->_curState = newState; this->_curTime = time(0);}voID Monkey::stop(){ log("stop()");}voID Monkey::walk(){ this->_curPos += this->_curStep; this->sp->setposition(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_有限状态机_1全部内容,希望文章能够帮你解决Cocos2dx_有限状态机_1所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)