Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植8: Jumping and Crouching
来源:互联网 发布:淘宝网店的等级 编辑:程序博客网 时间:2024/05/11 04:55
尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17383997
<捕鱼达人>回顾
【cocos2d-x IOS游戏开发-捕鱼达人1】内容介绍
<城市跑酷>回顾
【cocos2d-x IOS游戏开发-城市跑酷1】跑酷游戏介绍
上节回顾
Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植7:Gesture Recognizer【手势识别】
这一节中将介绍如何添加一些跑酷游戏的常用的一些控制方式。
在修改runner类之前首先要在Utils.h中添加如下代码
// collision type for chipmunkenum SpriteTag{ SpriteTagRunner, SpriteTagCoin, SpriteTagRock,};
需要定义一个标记chipmunk碰撞检测的枚举类型
打开Runner.cpp,通过添加如下代码来完成RunnerStat的定义:
// define enum for runner status//1.定义一个runner状态的枚举,跑步者有许多状态enum RunnerStat{ RunnerStatRunning, RunnerStatJumpUp, RunnerStatJumpDown, RunnerStatCrouch, RunnerStatIncredible,};
为runner类定义几个新的成员变量
CCSize crouchSize;cpSpace *space;cpBody *body;// current chipmunk bodycpShape *shape;// current chipmunk shapestatic RunnerStat stat;// init with running statusCCAction *runningAction;CCAction *jumpUpAction;CCAction *jumpDownAction;CCAction *crouchAction;
当游戏角色蹲下的时候它的形状将会发生改变,下面的代码会记录蹲下时候的大小。 在init()中添加如下代码:
// init crouchSizeCCPhysicsSprite *tmpSprite = CCPhysicsSprite::createWithSpriteFrameName("runnerCrouch0.png");crouchSize = tmpSprite->getContentSize();改变initShape
// start with running shape //7.在initShape里,创建与精灵大小相等的chipmunk形状 initShape("running");
当然还要修改initShape()中的代码。用下面的代码替换它的内容:
void Runner::initShape(const char* type) {if (shape) {//space.removeShape(this.shape);cpSpaceRemoveShape(space, shape);}if (type == "running") {shape = cpBoxShapeNew(body,runningSize.width - 14, runningSize.height);} else {// crouchshape = cpBoxShapeNew(body,crouchSize.width, crouchSize.height);}//shape = cpBoxShapeNew(body,//runningSize.width, runningSize.height);//shape->setCollisionType(SpriteTag.runner);shape->collision_type = SpriteTagRunner;cpSpaceAddShape(space, shape);}还有initAction()中的三个动画初始化函数:jumpUpAction, jumpDownAction和crouchAction
// init jumpUpActionCCAnimation* animation1;animation1 = CCAnimation::create();CCSpriteFrame * frame1;// num equal to spriteSheetfor (unsigned int i = 0; i < 4; i++) {char str[100] = {0};sprintf(str, "runnerJumpUp%i.png", i);frame1 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);animation1->addSpriteFrame(frame1);}animation1->setDelayPerUnit(0.2f);jumpUpAction = CCAnimate::create(animation1);jumpUpAction->retain();// init jumpDownActionCCAnimation* animation2;animation2 = CCAnimation::create();CCSpriteFrame * frame2;// num equal to spriteSheetfor (unsigned int i = 0; i < 2; i++) {char str[100] = {0};sprintf(str, "runnerJumpDown%i.png", i);frame2 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);animation2->addSpriteFrame(frame);}animation2->setDelayPerUnit(0.2f);jumpDownAction = CCAnimate::create(animation2);jumpDownAction->retain();// init crouchActionCCAnimation* animation3;animation3 = CCAnimation::create();CCSpriteFrame * frame3;// num equal to spriteSheetfor (unsigned int i = 0; i < 1; i++) {char str[100] = {0};sprintf(str, "runnerCrouch%i.png", i);frame3 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);animation3->addSpriteFrame(frame3);}animation3->setDelayPerUnit(0.3f);crouchAction = CCAnimate::create(animation3);crouchAction->retain();
已经完成了初始化,然后要做的是在Runner类中添加下面的函数:
void Runner::jump(){if (stat == RunnerStatRunning) {//this.body.applyImpulse(cp.v(0, 250), cp.v(0, 0));cpVect arg1 = cpv(0, 250); cpVect arg2 = cpv(0, 0); cpBodyApplyImpulse((cpBody*)body , (cpVect)arg1 , (cpVect)arg2);stat = RunnerStatJumpUp;sprite->stopAllActions();sprite->runAction(jumpUpAction);//播放音效//audioEngine->playEffect(s_music_jump);}}
只需要给runner的body一个向上的冲力,runner就会跳起来,在将动画换成跳跃动画之前,通过调用sprite->stopAllActions()来停止当前动画。
跳跃的动作可以分为两个部分--上升和下降。可以通过观察body的重心的线速度来检测从上升到下降状态的转换。
如果在Y轴的线速度小于0.1,跳跃的动作正在从上升状态切换到下降状态,这时将精灵的动画切换到jumpDownAction.
如果Y轴的线速度等于0说明精灵的从下降的状态切换到了跑动的状态,这时将精灵动画改成runningAction.
void Runner::step(float dt){//线速度cpVect vel = body->v;if (stat == RunnerStatJumpUp) {if (vel.y < 0.1) {stat = RunnerStatJumpDown;sprite->stopAllActions();sprite->runAction(jumpDownAction);}return;}if (stat == RunnerStatJumpDown) {if (vel.y == 0) {stat = RunnerStatRunning;sprite->stopAllActions();sprite->runAction(runningAction);}return;}}
在蹲下的时候,只需要修改body的shape.把下面的函数添加到Runner类中:
void Runner::crouch(){if (stat == RunnerStatRunning) {initShape("crouch");sprite->stopAllActions();sprite->runAction(crouchAction);stat = RunnerStatCrouch;// after time turn to running statscheduleOnce(schedule_selector(Runner::loadNormal), 1.5f);//audioEngine.playEffect(s_music_crouch);}}
蹲下的状态不会持续太长的时间,可以通过调用
scheduleOnce(schedule_selector(Runner::loadNormal), 1.5f);来返回到跑动状态.
loadNormal() 初始化跑动状态下body的shape.可以这样做:
void Runner::loadNormal(float dt){initShape("running");sprite->stopAllActions();sprite->runAction(runningAction);stat = RunnerStatRunning;}
现在已经完成了Runner.cpp,用下面代码替换PlayScene.cpp中的onTouchEnded函数:
switch (rtn) {case SimpleGesturesUp:CCLOG("Runner::jump");runner->jump();break;case SimpleGesturesDown:CCLOG("Runner::crouch");runner->crouch();break;case SimpleGesturesNotSupport:case SimpleGesturesError:// try dollar Recognizer// 0:Use Golden Section Search (original) // 1:Use Protractor (faster)#if 0var result = dollar.Recognize(this.recognizer.getPoints(), 1);CCLOG(result.Name);if (result.Name == "circle") {runner->incredibleHulk();}#endifCCLOG("not support or error touch,use geometricRecognizer!!");runner->incredibleHulk();#if 0//通过GeometricRecognizer校准//可以选择屏蔽玩家单击操作if (p_2dPath.size() < 1){return ;}RecognitionResult r = geometricRecognizer->recognize(p_2dPath);if((r.name != "Unknown") && (r.score > 0.5)){runner->incredibleHulk();//return;}#endif
添加下面代码来切换动画:
// runner step, to change animationrunner->step(delta);
编译运行之,上下滑动来看看跳跃和蹲下的效果。
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植8: Jumping and Crouching
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植10:Adding Coins and Rocks【添加金币和障碍物】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植4:游戏主场景的介绍
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植1:环境搭建及Parkour介绍
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植5:初始化物理世界【Chipmunk】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植6:Running This Way
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植7:Gesture Recognizer【手势识别】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植9:Map Loop【地图循环加载】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植3:游戏主菜单界面的实现
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植11:添加金币和障碍物到地图中
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植2:如何新建一个项目,引擎目录结构介绍
- 泰然网Cocos2d-x文档教程和其他源码
- jni cocos2d-x游戏引擎移植到android:helloworld
- cocos2d-x游戏iphone移植到android需要注意事项
- 将cocos2d-x游戏移植到win8/wp8上
- 将cocos2d-x游戏移植到win8/wp8上
- 安卓版的cocos2d-x游戏移植到ios
- 将cocos2d-x游戏移植到win8/wp8上
- 参加广州网页平面UI设计培训让你成为企业精英
- 使用CGI进行远程编辑
- vim中NERDTree 的安装和使用
- 供应商,地址,业务实体,地点关联银行账户
- ubuntu安装
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植8: Jumping and Crouching
- 1018 _剪子包袱锤
- linux解压rar文件
- Android Loaders简介
- FindFirstFile与FindNextFile的用法
- MySQL中的数据类型
- Dijkstra算法
- Oracle BBED
- java socket远程屏幕监视(一)