cocos2dx 3.3 跑酷游戏 人物的创建
2014-10-22 21:44
369 查看
人物的创建之前先建个头文件用来放一些设定Def.h(这是我后期写好了,以后想到哪些设定就在这里修改):
#pragma once //人物的运动状态 const int STATE_PLAYER_RUN = 0; const int STATE_PLAYER_JUMP = 1; const int STATE_PLAYER_DOWN = 2; const int STATE_PLAYER_STOP = 3; const int STATE_PLAYER_DIE = 4; //人物速度和加速度 const double DEFLAUT_SPEEDY = 4; const double DEFLAUT_ACCE = 0.1; //地图左移大小(速度) const int MOVING_GAP = 8; //人物动作动画tag const int TAG_RUN = 1; const int TAG_JUMP = 2; const int TAG_DOWN = 3; #define MUSIC_GOLD "gold.mp3" #define MUSIC_TOUCH "touch.wav" #define MUSIC_BACKGROUND "background.mp3" #define MUSIC_JUMP "jump.mp3"注意新建项的存放位置是classes文件夹下游戏中只有一个人物,所以打算使用单例模式,模仿Director建立静态的getInstance();人物应该有跑动,跳跃,落下,死亡4个状态,目前是这4个,有可能会增加;人物跳跃时y速度和加速度很难定,试了很多次,现在的效果还不是很好
#pragma once #include "cocos2d.h" #include <string> #include "Def.h"//设定文件放在该头文件中 using std::string; USING_NS_CC; class Player :public Sprite { public: Player(){} ~Player(){} static Player *getInstance();//建立静态成员变量,返回初始化好的一个静态的sharePlayer*; bool setPlayState(int PlayState);//设置人物状态 int getPlayState();//获得人物状态,根据需要来写 void jump();//处理跳跃 void run();// void down();// void setSpeed(double v);//设置速度。。。没用到了后来。。。弃 private: static Player *sharePlayer; bool createPlayer();//创建人物 bool init();//初始化 Animation *animationRun;//人物运动动画 bool m_RunActionIng; Animation *animationJump;//弃。。。决定只用一张图 Animation *animationDown;//弃。。。 AnimationCache *animationCache;//动画缓冲 SpriteFrameCache *frameCache; int m_playerState;//人物状态 double speedy;//y速度 double accelerate;//y加速度 void actionCallBack(float f); };Player.cpp:照例初始化先
#include "Player.h" Player *Player::sharePlayer = nullptr;//类静态成员变量的初始化 Player *Player::getInstance() { if (sharePlayer == NULL) { sharePlayer = new Player; if (!sharePlayer->init()) { delete sharePlayer; sharePlayer = nullptr; } } return sharePlayer; } bool Player::init() { //Sprite::init(); m_playerState = STATE_PLAYER_RUN; speedy = DEFLAUT_SPEEDY;//极其麻烦的速度设置 accelerate = DEFLAUT_ACCE; frameCache = SpriteFrameCache::getInstance(); frameCache->addSpriteFramesWithFile("Resources_Pack_0.plist");//添加精灵帧缓存 createPlayer(); return true; }人物和动画的创建:
bool Player::createPlayer() { Sprite::initWithSpriteFrame(frameCache->getSpriteFrameByName("run01.png")); animationRun = Animation::create(); for (int i = 1; i < 10; i++) { string s = "run0" + std::to_string(i) + ".png"; animationRun->addSpriteFrameWithFileName(s); } animationRun->addSpriteFrameWithFileName("run10.png"); animationRun->addSpriteFrameWithFileName("run11.png"); animationRun->setDelayPerUnit(0.1f); animationRun->setLoops(-1); auto animateRun = Animate::create(animationRun); animateRun->setTag(TAG_RUN); // runAction(animateRun); m_RunActionIng = false;//这里要设置一个变量,判断人物是否在run,由不在到在要运行动画,否则不运行 animationCache = AnimationCache::getInstance(); animationCache->addAnimation(animationRun, "run"); //人物运动定时器 this->schedule(schedule_selector(Player::actionCallBack), 0.016);//利用状态机,每帧检测人物状态来判断该进行什么运动 return true; }
void Player::actionCallBack(float f) { switch (m_playerState) { case STATE_PLAYER_RUN: run(); break; case STATE_PLAYER_JUMP: jump(); break; case STATE_PLAYER_DOWN: down(); break; default: break; } }
人物运动状态函数的实现
void Player::jump(){this->stopActionByTag(TAG_RUN); //通过标记来停止动画m_RunActionIng = false; //表示下次人物run 的话要运行动画了this->setSpriteFrame(SpriteFrame::create("run03.png",Rect(0,0,64,48))); //设置人物跳起来时的画面int x = getPositionY();this->setPositionY(getPositionY()+speedy); //改变人物y值,使其向上运动speedy -= accelerate; //速度减小if (speedy<=0) //速度小于等于零则要下落{m_playerState = STATE_PLAYER_DOWN;//speedy = DEFLAUT_SPEEDY; //改到其他地方去了,}//CCLOG("IS JUMPING");}void Player::down(){stopActionByTag(TAG_RUN); //即使没有run的动画,停止也没事m_RunActionIng = false; //同上setPositionY(getPositionY() - speedy);speedy += accelerate;//下落的状态改变会有碰撞检测来实现// CCLOG("is DOWNING");}void Player::run()//运动{// CCLOG("IS RUN");if (m_RunActionIng == false)//动画不在播放才能播放动画{Animate *animate = Animate::create(animationCache->getAnimation("run"));animate->setTag(TAG_RUN);runAction(animate);//运行动画m_RunActionIng = true;}//int xx = this->getPositionX();//调试信息// int yy = this->getPositionY();// CCLOG("x=%d y=%d", xx, yy);}
bool Player::setPlayState(int PlayState){this->m_playerState = PlayState;return true;}int Player::getPlayState(){return m_playerState;}void Player::setSpeed(double v){speedy = v;}
相关文章推荐
- cocos2dx 3.3 跑酷游戏 声音的加入
- cocos2dx 3.3 跑酷游戏的编写
- cocos2dx 3.3 跑酷游戏 背景层
- cocos2dx 3.3 跑酷游戏 信息层
- cocos2dx 3.3 跑酷游戏的编写 游戏层
- cocos2dx 3.3 魂斗罗初步尝试 游戏层(暂停段时间,以后再写)。。。
- wow 人物初次创建进入游戏时的世界公告
- 【iOS-cocos2d-X 游戏开发之四】Cocos2dX创建Android NDK新项目并编译导入Eclipse中正常运行!
- cocos2dx_3.3 lua类的创建
- 笔记:利用 Cocos2dx 3.2 与 Box2D制作一个跑酷游戏(上)
- 创建cocos2dx项目时,加载已有游戏时不能真机测试的问题
- cocos2dx 3.3 + QT5.3制作游戏编辑器
- Cocos2dx系列笔记7:一个简单的跑酷游戏《萝莉快跑》的消化(附下载)
- 《游戏脚本的设计与开发》-(RPG部分)3.3 加入多个人物以及对话实现
- 【cocos2dx-3.0beta-制作flappybird】玄关是必要的—游戏欢迎界面的创建
- Cocos2dx游戏开发系列笔记7:一个简单的跑酷游戏《萝莉快跑》的消化(附下载)
- Mac下创建多平台cocos2dx游戏项目
- cocos2d-x游戏开发 跑酷(三) 人物跑动
- 【iOS-cocos2d-X 游戏开发之四】Cocos2dX创建Android NDK新项目并编译导入Eclipse中正常运行!
- Unity3D+免费工具开发2D游戏之三:创建人物动画