[置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动
cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动
写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不管是简单的还是困难的现在都慢慢的一步一步克服了,其实用cocos2d-x做游戏特别的简单,大家不要被是做游戏吓到了,支持我把游戏开源的原因是因为 eoe上海 的大家的支持。加油,加油,要说明一下的是,写得不好还请大家多多包含哦。相信你们跟着我得博客,一步一步做肯定会成功的。
看到有朋友跟这我做,感到很开心这样我才有写下去的信心,大家一起加油,主角写好了几天了一直没有更新,今天给大家更新送到,下次更新看到家的评论。
好了回到正题,今天开始主角的上下移动的控制
效果图片
首先来添加主角类
SWGamePlayer.h
//
// SWGamePlayer.h
// Holdtail
//
// Created by 帅 印 on 13-12-04.
//
// #ifndef __Holdtail__SWGamePlayer__
#define __Holdtail__SWGamePlayer__ #include <iostream>
#include <sstream>
#include "cocos2d.h"
#include "cocos-ext.h" using namespace cocos2d;
using namespace std;
using namespace extension; //类型转换类int.float --> String
template<typename T>
string Convert2String(const T & value){
stringstream ss;
ss << value;
return ss.str();
} //定义图片的枚举的类型
typedef enum{
tag_player_up,
tag_player_down,
tag_player_fly,
tag_player_run,
tag_playerHp,
}tagPlayers; class SWGamePlayer:public cocos2d::Sprite{
public:
static SWGamePlayer *createPlayer(const char *fileName,int allCount,float sprit);
//降低主角的血量
void downHp(float _value);
int hp;//血量
int hpMax;//最高血量
static SWGamePlayer *sharedPlayer(); void callbackForDown();
private:
void playerInit(const char *fileName,int allCount,float sprit);
//精灵的动态表现 文件名称 文件帧数 精灵速度
void createAnimate(const char *fileName,int allCount,float sprit); bool isStrong;
int strongCount;
int strongTime;
void strongIng(float soso); };
#endif /* defined(__Holdtail__SWGamePlayer__) */
SWGamePlayer.m
//
// SWGamePlayer.cpp
// Holdtail
//
// Created by 帅 印 on 13-8-21.
//
// #include "SWGamePlayer.h"
#include "SWGameMap.h"
#include "SWGameWorld.h"
#include "SimpleAudioEngine.h"
#include "SWMenu.h" using namespace cocos2d;
using namespace CocosDenshion; //设置主角精灵的两种表现形式
static Sprite *flySprite;
static Sprite *runSprite; //声明静态变量
static SWGamePlayer *SWPL; SWGamePlayer *SWGamePlayer::sharedPlayer(){
if(SWPL != NULL){
return SWPL;
}
return NULL;
} SWGamePlayer *SWGamePlayer::createPlayer(const char* fileName,int allCount,float sprit){
SWGamePlayer *player = new SWGamePlayer(); if (player && player->initWithFile("nulls.png")) {
player->autorelease();
player->playerInit(fileName,allCount,sprit);
return player;
} CC_SAFE_DELETE(player);
return NULL; } void SWGamePlayer::playerInit(const char *fileName,int allCount,float sprit){ SWPL = this; Size size = Director::getInstance()->getWinSize(); //主角加入枪支
auto spgane = Sprite::create("gang01.png");
spgane->setPosition(Point(40, -10));
addChild(spgane); //创建动物的动画
createAnimate(fileName,allCount,sprit); //初始化主角的位置
this->setPosition(Point(size.width/8, size.height/2)); //设置血量
hp = 1000;
hpMax = 1000; ControlSlider* slider = ControlSlider::create("brood.png", "broods.png", "null.png");
slider->setPosition(Point(250, size.height-40));
slider->setTag(tag_playerHp);
/* 设置滑动条的范围 */
slider->setMinimumValue(0);
slider->setMaximumValue(hpMax);
/* 直接设置滑动条的当前值 */
slider->setValue(hp);
//slider->setTouchEnabled(false);
SWGameWorld::sharedWorld()->addChild(slider); //添加主角形象
Sprite *hmsprite = Sprite::create("gameicon.png");
hmsprite->setPosition(Point(50, size.height-40));
SWGameWorld::sharedWorld()->addChild(hmsprite); //添加游戏进行进度条
Sprite *timebg = Sprite::create("timelinebg.png");
timebg->setPosition(Point(670, size.height-35));
SWGameWorld::sharedWorld()->addChild(timebg); //添加游戏进行条动画效果
Sprite *timeplay = Sprite::create("timelineyou.png");
timeplay->setPosition(Point(510, size.height-40));
SWGameWorld::sharedWorld()->addChild(timeplay); //让动画进行播放
// ActionInterval* moveToGameOver = MoveBy::create(120, Point(320, 0));
// CallFunc * funCall =CallFunc::create(timeplay, callfunc_selector(SWGamePlayer::gameGoOver));
// FiniteTimeAction *seq =Sequence::create(moveToGameOver,funCall,NULL);
// seq->setTag(tag_player_down);
// timeplay->runAction(seq); // MenuItemImage *SPJN01ITEM = MenuItemImage::create("pausebutton.png", "pausedownbutton.png",this,menu_selector(SWGamePlayer::backMenu));
// SPJN01ITEM->setPosition(Point(420,280));
// Menu *menu = Menu::create(SPJN01ITEM,NULL);
// SWGameWorld::sharedWorld()->addChild(menu); } void SWGamePlayer::createAnimate(const char *fileName,int allCount,float sprit){ runSprite = Sprite::create("zhujuerun.png");
runSprite->setTag(tag_player_run);
Animation *animation = Animation::create();
Texture2D *texture = TextureCache::getInstance()->addImage("zhujuerun.png");
int eachWidth = runSprite->getContentSize().width/8;
for (int i = 0; i<8; i++) {
animation->addSpriteFrameWithTexture(texture, Rect(i*eachWidth, 0, eachWidth, this->getContentSize().height));
}
animation->setDelayPerUnit(0.1f);//必须设置否则不会动态播放
animation->setRestoreOriginalFrame(true);//是否回到第一个
animation->setLoops(-1);//重复次数
FiniteTimeAction *animaterun = Animate::create(animation);
runSprite->setPosition(this->getPosition());
runSprite->runAction(animaterun);
this->addChild(runSprite); flySprite = Sprite::create("zhujue.png");
flySprite->setTag(tag_player_fly);
Animation *animations = Animation::create();
Texture2D *textures = TextureCache::getInstance()->addImage("zhujue.png");
int eachWidths = flySprite->getContentSize().width/8;
for (int i = 0; i<8; i++) {
animations->addSpriteFrameWithTexture(textures, Rect(i*eachWidths, 0, eachWidths, this->getContentSize().height));
}
animations->setDelayPerUnit(0.05f);//必须设置否则不会动态播放
animations->setRestoreOriginalFrame(true);//是否回到第一个
animations->setLoops(-1);//重复次数
FiniteTimeAction *animateruns = Animate::create(animations);
flySprite->setPosition(this->getPosition());
flySprite->runAction(animateruns);
this->addChild(flySprite); runSprite->setOpacity(0);
flySprite->setOpacity(255);
} void SWGamePlayer::downHp(float _value){
//通过这个方法来检测是否有发生碰撞
//获取血量减掉血量
hp = hp-_value;
//将血量ICO进行更新
ControlSlider *slider = (ControlSlider *)SWGameWorld::sharedWorld()->getChildByTag(tag_playerHp);
slider->setValue(hp); //判断主角的血量是否已经达到了零的位置
if(0 >= hp){
Size size = Director::getInstance()->getWinSize();
LayerColor *layer = LayerColor::create(Color4B(0, 0, 0, 200), size.width, size.height);
Sprite *sp = Sprite::create("fail_bg.png");
sp->setPosition(Point(size.width*0.5, size.height*0.5));
layer->addChild(sp); }else{
//主角无敌时的闪的效果
isStrong = true;
strongCount = 0;
strongTime = 1*30;
this->schedule(schedule_selector(SWGamePlayer::strongIng));
}
} //无敌时间处理函数
void SWGamePlayer::strongIng(float soso){
strongCount++;
if(strongCount%strongTime == 0){
this->setVisible(true);
this->unschedule(schedule_selector(SWGamePlayer::strongIng));
}else{
//主角无敌时的闪的效果
if(strongCount%3 == 0){
this->setVisible(false);
}else{
this->setVisible(true);
}
}
} void SWGamePlayer::callbackForDown(){
//当开始让主角飞行时切换动画到行走模式
Sprite *run = (Sprite *)this->getChildByTag(tag_player_run);
run->setOpacity(255);
Sprite *fly = (Sprite *)this->getChildByTag(tag_player_fly);
fly->setOpacity(0);
}
然后在游戏世界里面加入对主角的配置
当然对游戏的世界类也需要做小小的修改
SWGameWorld.h
//
// SWGameWorld.h
// Holdtail
//
// Created by 帅 印 on 13-12-02.
//
// #ifndef __Holdtail__SWGameWorld__
#define __Holdtail__SWGameWorld__ #include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h" USING_NS_CC;
USING_NS_CC_EXT;
//定义属性
typedef enum {
tag_player
}tagWorld; class SWGameWorld:public cocos2d::Layer{
public:
static cocos2d::Scene *scene();
static SWGameWorld *sharedWorld(); void onTouchesBegan(const std::vector<Touch*>& touches, Event *event);
void onTouchesMoved(const std::vector<Touch*>& touches, Event *event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
private:
virtual bool init();
CREATE_FUNC(SWGameWorld);
};
#endif /* defined(__Holdtail__SWGameWorld__) */
SWGameWorld.cpp
//
// SWGameWorld.cpp
// Holdtail
//
// Created by 帅 印 on 13-12-02.
//
// #include "SWGameWorld.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "SWGameMap.h"
#include "SWGamePlayer.h" using namespace cocos2d;
using namespace CocosDenshion; //声明静态变量
static SWGameWorld *SWGW; SWGameWorld *SWGameWorld::sharedWorld(){
if(SWGW != NULL){
return SWGW;
}
return NULL;
} Scene *SWGameWorld::scene(){
Scene *scene = Scene::create();
SWGameWorld *layer = SWGameWorld::create();
scene->addChild(layer); return scene;
} //创建场景
bool SWGameWorld::init(){
if( !Layer::init()){
return false;
}
SWGW = this; //地图
SWGameMap *map = SWGameMap::createMap("cloudbg.png","cloud04.png","cloud03.png","cloud02.png","cloud01.png","treesbg.png");
addChild(map); //创建主角
SWGamePlayer *player = SWGamePlayer::createPlayer("zhujuerun.png", 8, 0.05f);
addChild(player,10,tag_player); setTouchEnabled(true);
//设置为单点响应
setTouchMode(Touch::DispatchMode::ALL_AT_ONCE); auto myListener = EventListenerTouchAllAtOnce::create();
myListener->onTouchesBegan = CC_CALLBACK_2(SWGameWorld::onTouchesBegan, this);
myListener->onTouchesMoved = CC_CALLBACK_2(SWGameWorld::onTouchesMoved, this);
myListener->onTouchesEnded = CC_CALLBACK_2(SWGameWorld::onTouchesEnded, this); return true;
} void SWGameWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event){ for (int i = 0; i<touches.size(); i++) {
if (0 == i) {
SWGameMap::updateLayerHeight(0);
}
}
}
void SWGameWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event){ }
void SWGameWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event){
for (int i = 0; i<touches.size(); i++) {
if (0 == i) {
SWGameMap::updateLayerHeight(1);
}
}
}
好了这下,主角就可以上下移动了,原理就是控制背景的移动,从而达到主角的移动效果
今天的这个阶段的项目打包下载-->飞机直达
[置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 004.[HoldTail]主角的上下飞行跟移动的更多相关文章
- [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面
cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不 ...
- [置顶] 很荣幸被选为2013年度 CSDN博客之星评选,如果觉得我的文章可以,请投我一票!
亲爱的小伙伴们,很荣幸我被选为<2013年度CSDN博客之星候选人>,希望大家多多支持,geekguy会继续努力,为大家奉献更好的文章. 投票地址:http://vote.blog.csd ...
- 一步步开发自己的博客 .NET版(1、基本显示)
前言 我们每个猿都有一个搭建自己独立博客的梦,我也不例外.以前想 现在想 以后也想.之所以一直迟迟没有着手,是因为难以跨出第一步.每次心里想着,等我以后技术好了再说,然后就没有然后了.以前用过word ...
- 一步步开发自己的博客 .NET版(4、文章发布功能)百度编辑器
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做个插件,任何网站上的技术 ...
- 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做 ...
- 一步步开发自己的博客 .NET版 剧终篇(6、响应式布局 和 自定义样式)
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做 ...
- 基于.NET Core开发的个人博客发布至CentOS小计
早些时候,使用 .NET Framework 开发网站,只能部署在 Windows 服务器上面,近两年 .NET Core 如火如荼,乘此机会赶紧上车,最近将自己利用 .NET Core 开发的个人博 ...
- Node.js 从零开发 web server博客项目[express重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[数据存储]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
随机推荐
- Mybatis3 框架理解
最近工作比较闲,维护一个政府机关的短信发送平台,大部分业务逻辑都在Oracle数据库上,但自己明明应聘的是Java开发啊!!!整天写存储过程的我还是有一颗写高级语言的心啊!!!好吧!!!先找个数据库方 ...
- Python笔记之基本的语法
1 变量和赋值 Python是动态类型语言,不须要预先声明变量的类型.变量的类型在赋值的那一刻被初始化. Python变量名是大写和小写敏感的,即"cAsE"与"CaSe ...
- Android之Camera控制拍照
package com.android.xiong.cameratest; import java.io.File; import java.io.FileOutputStream; import j ...
- 解决win7 中source insight没有courier new字节的问题
解决win7 中source insight没有courier new字节的问题 http://blog.csdn.net/season_hangzhou/article/details/18665 ...
- 网页制作之html基础学习3-css样式表
样式:CSS(Cascading Style Sheets,层叠样式表),作用是美化HTML网页. 在样式里面用 /* */ 进行注释. 1.样式表的基本概念 1.1.样式表分类 1.内联样式表 和 ...
- sencha touch(7)——list组件
1.list组件是一个很强大的组件.用于以一览表的形式或者列表的形式展示应用程序中的大量的数据.该组件使用XTemplate模版来显示数据,同时与数据仓库进行绑定.所以当数据仓库中的数据发生变化的时候 ...
- java--继承的一些笔记
public class Person { public void display(){ System.out.println("Play Person..."); } stati ...
- 高斯拉普拉斯算子(Laplace of Gaussian)
高斯拉普拉斯(Laplace of Gaussian) kezunhai@gmail.com http://blog.csdn.net/kezunhai Laplace算子作为一种优秀的边缘检测算子, ...
- bin文件格式分析
xip 的 bin 文件分析 一个bin 文件在存储上是按以下的结构存储的 组成:标记(7)+Image開始地址(1)+Image长度(1) 记录0地址+记录0长+记录0校验和+ ...
- 搭建Windows SVN服务器及TortoiseSVN使用帮助和下载
搭建Windows SVN服务器: 用的SVN服务器通常为外部,例如Google Code的服务器,不过,做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行 ...