cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动

写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不管是简单的还是困难的现在都慢慢的一步一步克服了,其实用cocos2d-x做游戏特别的简单,大家不要被是做游戏吓到了,支持我把游戏开源的原因是因为  eoe上海  的大家的支持。加油,加油,要说明一下的是,写得不好还请大家多多包含哦。相信你们跟着我得博客,一步一步做肯定会成功的。

看到有朋友跟这我做,感到很开心这样我才有写下去的信心,大家一起加油,主角写好了几天了一直没有更新,今天给大家更新送到,下次更新看到家的评论。

好了回到正题,今天开始主角的上下移动的控制

效果图片

首先来添加主角类

SWGamePlayer.h

  1. //
  2. // SWGamePlayer.h
  3. // Holdtail
  4. //
  5. // Created by 帅 印 on 13-12-04.
  6. //
  7. //
  8.  
  9. #ifndef __Holdtail__SWGamePlayer__
  10. #define __Holdtail__SWGamePlayer__
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. #include "cocos2d.h"
  15. #include "cocos-ext.h"
  16.  
  17. using namespace cocos2d;
  18. using namespace std;
  19. using namespace extension;
  20.  
  21. //类型转换类int.float --> String
  22. template<typename T>
  23. string Convert2String(const T & value){
  24. stringstream ss;
  25. ss << value;
  26. return ss.str();
  27. }
  28.  
  29. //定义图片的枚举的类型
  30. typedef enum{
  31. tag_player_up,
  32. tag_player_down,
  33. tag_player_fly,
  34. tag_player_run,
  35. tag_playerHp,
  36. }tagPlayers;
  37.  
  38. class SWGamePlayer:public cocos2d::Sprite{
  39. public:
  40. static SWGamePlayer *createPlayer(const char *fileName,int allCount,float sprit);
  41. //降低主角的血量
  42. void downHp(float _value);
  43. int hp;//血量
  44. int hpMax;//最高血量
  45. static SWGamePlayer *sharedPlayer();
  46.  
  47. void callbackForDown();
  48. private:
  49. void playerInit(const char *fileName,int allCount,float sprit);
  50. //精灵的动态表现 文件名称 文件帧数 精灵速度
  51. void createAnimate(const char *fileName,int allCount,float sprit);
  52.  
  53. bool isStrong;
  54. int strongCount;
  55. int strongTime;
  56. void strongIng(float soso);
  57.  
  58. };
  59. #endif /* defined(__Holdtail__SWGamePlayer__) */

SWGamePlayer.m

  1. //
  2. // SWGamePlayer.cpp
  3. // Holdtail
  4. //
  5. // Created by 帅 印 on 13-8-21.
  6. //
  7. //
  8.  
  9. #include "SWGamePlayer.h"
  10. #include "SWGameMap.h"
  11. #include "SWGameWorld.h"
  12. #include "SimpleAudioEngine.h"
  13. #include "SWMenu.h"
  14.  
  15. using namespace cocos2d;
  16. using namespace CocosDenshion;
  17.  
  18. //设置主角精灵的两种表现形式
  19. static Sprite *flySprite;
  20. static Sprite *runSprite;
  21.  
  22. //声明静态变量
  23. static SWGamePlayer *SWPL;
  24.  
  25. SWGamePlayer *SWGamePlayer::sharedPlayer(){
  26. if(SWPL != NULL){
  27. return SWPL;
  28. }
  29. return NULL;
  30. }
  31.  
  32. SWGamePlayer *SWGamePlayer::createPlayer(const char* fileName,int allCount,float sprit){
  33. SWGamePlayer *player = new SWGamePlayer();
  34.  
  35. if (player && player->initWithFile("nulls.png")) {
  36. player->autorelease();
  37. player->playerInit(fileName,allCount,sprit);
  38. return player;
  39. }
  40.  
  41. CC_SAFE_DELETE(player);
  42. return NULL;
  43.  
  44. }
  45.  
  46. void SWGamePlayer::playerInit(const char *fileName,int allCount,float sprit){
  47.  
  48. SWPL = this;
  49.  
  50. Size size = Director::getInstance()->getWinSize();
  51.  
  52. //主角加入枪支
  53. auto spgane = Sprite::create("gang01.png");
  54. spgane->setPosition(Point(40, -10));
  55. addChild(spgane);
  56.  
  57. //创建动物的动画
  58. createAnimate(fileName,allCount,sprit);
  59.  
  60. //初始化主角的位置
  61. this->setPosition(Point(size.width/8, size.height/2));
  62.  
  63. //设置血量
  64. hp = 1000;
  65. hpMax = 1000;
  66.  
  67. ControlSlider* slider = ControlSlider::create("brood.png", "broods.png", "null.png");
  68. slider->setPosition(Point(250, size.height-40));
  69. slider->setTag(tag_playerHp);
  70. /* 设置滑动条的范围 */
  71. slider->setMinimumValue(0);
  72. slider->setMaximumValue(hpMax);
  73. /* 直接设置滑动条的当前值 */
  74. slider->setValue(hp);
  75. //slider->setTouchEnabled(false);
  76. SWGameWorld::sharedWorld()->addChild(slider);
  77.  
  78. //添加主角形象
  79. Sprite *hmsprite = Sprite::create("gameicon.png");
  80. hmsprite->setPosition(Point(50, size.height-40));
  81. SWGameWorld::sharedWorld()->addChild(hmsprite);
  82.  
  83. //添加游戏进行进度条
  84. Sprite *timebg = Sprite::create("timelinebg.png");
  85. timebg->setPosition(Point(670, size.height-35));
  86. SWGameWorld::sharedWorld()->addChild(timebg);
  87.  
  88. //添加游戏进行条动画效果
  89. Sprite *timeplay = Sprite::create("timelineyou.png");
  90. timeplay->setPosition(Point(510, size.height-40));
  91. SWGameWorld::sharedWorld()->addChild(timeplay);
  92.  
  93. //让动画进行播放
  94. // ActionInterval* moveToGameOver = MoveBy::create(120, Point(320, 0));
  95. // CallFunc * funCall =CallFunc::create(timeplay, callfunc_selector(SWGamePlayer::gameGoOver));
  96. // FiniteTimeAction *seq =Sequence::create(moveToGameOver,funCall,NULL);
  97. // seq->setTag(tag_player_down);
  98. // timeplay->runAction(seq);
  99.  
  100. // MenuItemImage *SPJN01ITEM = MenuItemImage::create("pausebutton.png", "pausedownbutton.png",this,menu_selector(SWGamePlayer::backMenu));
  101. // SPJN01ITEM->setPosition(Point(420,280));
  102. // Menu *menu = Menu::create(SPJN01ITEM,NULL);
  103. // SWGameWorld::sharedWorld()->addChild(menu);
  104.  
  105. }
  106.  
  107. void SWGamePlayer::createAnimate(const char *fileName,int allCount,float sprit){
  108.  
  109. runSprite = Sprite::create("zhujuerun.png");
  110. runSprite->setTag(tag_player_run);
  111. Animation *animation = Animation::create();
  112. Texture2D *texture = TextureCache::getInstance()->addImage("zhujuerun.png");
  113. int eachWidth = runSprite->getContentSize().width/8;
  114. for (int i = 0; i<8; i++) {
  115. animation->addSpriteFrameWithTexture(texture, Rect(i*eachWidth, 0, eachWidth, this->getContentSize().height));
  116. }
  117. animation->setDelayPerUnit(0.1f);//必须设置否则不会动态播放
  118. animation->setRestoreOriginalFrame(true);//是否回到第一个
  119. animation->setLoops(-1);//重复次数
  120. FiniteTimeAction *animaterun = Animate::create(animation);
  121. runSprite->setPosition(this->getPosition());
  122. runSprite->runAction(animaterun);
  123. this->addChild(runSprite);
  124.  
  125. flySprite = Sprite::create("zhujue.png");
  126. flySprite->setTag(tag_player_fly);
  127. Animation *animations = Animation::create();
  128. Texture2D *textures = TextureCache::getInstance()->addImage("zhujue.png");
  129. int eachWidths = flySprite->getContentSize().width/8;
  130. for (int i = 0; i<8; i++) {
  131. animations->addSpriteFrameWithTexture(textures, Rect(i*eachWidths, 0, eachWidths, this->getContentSize().height));
  132. }
  133. animations->setDelayPerUnit(0.05f);//必须设置否则不会动态播放
  134. animations->setRestoreOriginalFrame(true);//是否回到第一个
  135. animations->setLoops(-1);//重复次数
  136. FiniteTimeAction *animateruns = Animate::create(animations);
  137. flySprite->setPosition(this->getPosition());
  138. flySprite->runAction(animateruns);
  139. this->addChild(flySprite);
  140.  
  141. runSprite->setOpacity(0);
  142. flySprite->setOpacity(255);
  143. }
  144.  
  145. void SWGamePlayer::downHp(float _value){
  146. //通过这个方法来检测是否有发生碰撞
  147. //获取血量减掉血量
  148. hp = hp-_value;
  149. //将血量ICO进行更新
  150. ControlSlider *slider = (ControlSlider *)SWGameWorld::sharedWorld()->getChildByTag(tag_playerHp);
  151. slider->setValue(hp);
  152.  
  153. //判断主角的血量是否已经达到了零的位置
  154. if(0 >= hp){
  155. Size size = Director::getInstance()->getWinSize();
  156. LayerColor *layer = LayerColor::create(Color4B(0, 0, 0, 200), size.width, size.height);
  157. Sprite *sp = Sprite::create("fail_bg.png");
  158. sp->setPosition(Point(size.width*0.5, size.height*0.5));
  159. layer->addChild(sp);
  160.  
  161. }else{
  162. //主角无敌时的闪的效果
  163. isStrong = true;
  164. strongCount = 0;
  165. strongTime = 1*30;
  166. this->schedule(schedule_selector(SWGamePlayer::strongIng));
  167. }
  168. }
  169.  
  170. //无敌时间处理函数
  171. void SWGamePlayer::strongIng(float soso){
  172. strongCount++;
  173. if(strongCount%strongTime == 0){
  174. this->setVisible(true);
  175. this->unschedule(schedule_selector(SWGamePlayer::strongIng));
  176. }else{
  177. //主角无敌时的闪的效果
  178. if(strongCount%3 == 0){
  179. this->setVisible(false);
  180. }else{
  181. this->setVisible(true);
  182. }
  183. }
  184. }
  185.  
  186. void SWGamePlayer::callbackForDown(){
  187. //当开始让主角飞行时切换动画到行走模式
  188. Sprite *run = (Sprite *)this->getChildByTag(tag_player_run);
  189. run->setOpacity(255);
  190. Sprite *fly = (Sprite *)this->getChildByTag(tag_player_fly);
  191. fly->setOpacity(0);
  192. }

然后在游戏世界里面加入对主角的配置

当然对游戏的世界类也需要做小小的修改

SWGameWorld.h

  1. //
  2. // SWGameWorld.h
  3. // Holdtail
  4. //
  5. // Created by 帅 印 on 13-12-02.
  6. //
  7. //
  8.  
  9. #ifndef __Holdtail__SWGameWorld__
  10. #define __Holdtail__SWGameWorld__
  11.  
  12. #include <iostream>
  13. #include "cocos2d.h"
  14. #include "cocos-ext.h"
  15.  
  16. USING_NS_CC;
  17. USING_NS_CC_EXT;
  18. //定义属性
  19. typedef enum {
  20. tag_player
  21. }tagWorld;
  22.  
  23. class SWGameWorld:public cocos2d::Layer{
  24. public:
  25. static cocos2d::Scene *scene();
  26. static SWGameWorld *sharedWorld();
  27.  
  28. void onTouchesBegan(const std::vector<Touch*>& touches, Event *event);
  29. void onTouchesMoved(const std::vector<Touch*>& touches, Event *event);
  30. void onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
  31. private:
  32. virtual bool init();
  33. CREATE_FUNC(SWGameWorld);
  34. };
  35. #endif /* defined(__Holdtail__SWGameWorld__) */

SWGameWorld.cpp

  1. //
  2. // SWGameWorld.cpp
  3. // Holdtail
  4. //
  5. // Created by 帅 印 on 13-12-02.
  6. //
  7. //
  8.  
  9. #include "SWGameWorld.h"
  10. #include "cocos2d.h"
  11. #include "SimpleAudioEngine.h"
  12. #include "SWGameMap.h"
  13. #include "SWGamePlayer.h"
  14.  
  15. using namespace cocos2d;
  16. using namespace CocosDenshion;
  17.  
  18. //声明静态变量
  19. static SWGameWorld *SWGW;
  20.  
  21. SWGameWorld *SWGameWorld::sharedWorld(){
  22. if(SWGW != NULL){
  23. return SWGW;
  24. }
  25. return NULL;
  26. }
  27.  
  28. Scene *SWGameWorld::scene(){
  29. Scene *scene = Scene::create();
  30. SWGameWorld *layer = SWGameWorld::create();
  31. scene->addChild(layer);
  32.  
  33. return scene;
  34. }
  35.  
  36. //创建场景
  37. bool SWGameWorld::init(){
  38. if( !Layer::init()){
  39. return false;
  40. }
  41. SWGW = this;
  42.  
  43. //地图
  44. SWGameMap *map = SWGameMap::createMap("cloudbg.png","cloud04.png","cloud03.png","cloud02.png","cloud01.png","treesbg.png");
  45. addChild(map);
  46.  
  47. //创建主角
  48. SWGamePlayer *player = SWGamePlayer::createPlayer("zhujuerun.png", 8, 0.05f);
  49. addChild(player,10,tag_player);
  50.  
  51. setTouchEnabled(true);
  52. //设置为单点响应
  53. setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
  54.  
  55. auto myListener = EventListenerTouchAllAtOnce::create();
  56. myListener->onTouchesBegan = CC_CALLBACK_2(SWGameWorld::onTouchesBegan, this);
  57. myListener->onTouchesMoved = CC_CALLBACK_2(SWGameWorld::onTouchesMoved, this);
  58. myListener->onTouchesEnded = CC_CALLBACK_2(SWGameWorld::onTouchesEnded, this);
  59.  
  60. return true;
  61. }
  62.  
  63. void SWGameWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event){
  64.  
  65. for (int i = 0; i<touches.size(); i++) {
  66. if (0 == i) {
  67. SWGameMap::updateLayerHeight(0);
  68. }
  69. }
  70. }
  71. void SWGameWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event){
  72.  
  73. }
  74. void SWGameWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event){
  75. for (int i = 0; i<touches.size(); i++) {
  76. if (0 == i) {
  77. SWGameMap::updateLayerHeight(1);
  78. }
  79. }
  80. }

好了这下,主角就可以上下移动了,原理就是控制背景的移动,从而达到主角的移动效果

今天的这个阶段的项目打包下载-->飞机直达

[置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动的更多相关文章

  1. [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...

  2. [置顶] 很荣幸被选为2013年度 CSDN博客之星评选,如果觉得我的文章可以,请投我一票!

    亲爱的小伙伴们,很荣幸我被选为<2013年度CSDN博客之星候选人>,希望大家多多支持,geekguy会继续努力,为大家奉献更好的文章. 投票地址:http://vote.blog.csd ...

  3. 一步步开发自己的博客 .NET版(1、基本显示)

    前言 我们每个猿都有一个搭建自己独立博客的梦,我也不例外.以前想 现在想 以后也想.之所以一直迟迟没有着手,是因为难以跨出第一步.每次心里想着,等我以后技术好了再说,然后就没有然后了.以前用过word ...

  4. 一步步开发自己的博客 .NET版(4、文章发布功能)百度编辑器

    前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做个插件,任何网站上的技术 ...

  5. 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  6. 一步步开发自己的博客 .NET版 剧终篇(6、响应式布局 和 自定义样式)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  7. 基于.NET Core开发的个人博客发布至CentOS小计

    早些时候,使用 .NET Framework 开发网站,只能部署在 Windows 服务器上面,近两年 .NET Core 如火如荼,乘此机会赶紧上车,最近将自己利用 .NET Core 开发的个人博 ...

  8. Node.js 从零开发 web server博客项目[express重构博客项目]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  9. Node.js 从零开发 web server博客项目[数据存储]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

随机推荐

  1. Struts2 学习笔记16 struts标签 part2

    接下来说一下if标签.下面是结果图. <li><s:if test="#parameters.age[0]<0">error!</s:if> ...

  2. Redis性能调优:保存SNAPSHOT对性能的影响

    前一段时间.开发环境反馈,Redisserver訪问很慢,每一个请求要数秒时间,重新启动之后2~3天又会这样. 我查看了一下Linux的性能,没有什么问题. 通过 # redis-cli --late ...

  3. Python学习-使用matplotlib画动态多图

    最近常常使用matplotlib进行数学函数图的绘制,可是怎样使用matplotlib绘制动态图,以及绘制动态多图.直到今天才学会. 1.參考文字 首先感谢几篇文字的作者.帮我学会了怎样绘制.大家也能 ...

  4. arm: 使用结构体操作寄存器

    使用结构体操作寄存器: //寄存器赋值和取值的时候,要注意寄存器的长度,有的寄存器的值只有8位. //还要注意,使用volatile修饰寄存器变量.volatile 参考http://www.cnbl ...

  5. SDK Hello world(直接使用SDK封装)

    前言 将代码拆分了一下, 如果处理更多的消息也不怕看的眼花 SDK编程就是对各种Windows消息的处理 实验工程 /// @file exam_1.cpp /// @brief 查阅本地MSDN,  ...

  6. JVM调优总结(六)-分代垃圾回收详述2

    分代垃圾回收流程示意 选择合适的垃圾收集算法 串行收集器 用单线程处理所有垃圾回收工作,因为无需多线程交互,所以效率比较高.但是,也无法使用多处理器的优势,所以此收集器适合单处理器机器.当然,此收集器 ...

  7. c语言,结构体里面的函数

    以linux-3.2内核代码为例,结构体里面的函数的用法: 例,在某驱动文件中,定义了一个平台设备驱动: static struct platform_driver s3c24xx_led_drive ...

  8. POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

    Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 Des ...

  9. Apache Thrift的简单使用

    Apache Thrift的简单使用 ---------------------- 1. 简介 Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架.它有一个代码生成器来对它所 ...

  10. Java ArrayList add(int index, E element) example

    Simple add() method is used for adding an element at the end of the list however there is another va ...