嗯,至此我们的《跑跑跑》游戏已经基本完成了…才怪啊喂~!
来,主角不能只往前跑,我们得让他可以上下跑。
1. 制作新的移动控制器
我们要改变主角的移动行为,这很简单,因为聪明的我们早就料到了客户的需求是随时改变的(噗,那个客户说的是我么?),所以,我们使用了组合的方式。
现在是见证组合有多强大的时候了!我们只需要新建一个控制器,然后替换掉原来的SimpleMoveController,绑定到主角身上,就OK了~
2. 三方移动控制器
这个新的移动控制器可以往前、往上、往下移动,所以我给它命名为三方控制器。英文名叫做SanFangController,噗,开玩笑的,还是叫做ThreeDirectionController吧~
首先,我有个糟糕的事情想告诉大家,我们要修改Controller.h文件:
class Controller : public CCLayer {
其实没有修改什么,就是把Controller的父类由CCNode改为CCLayer。为什么呢?因为我们的新控制器希望获得屏幕触摸事件,而CCNode是获取不了这个事件的。
好,我们来创建三方移动控制器吧:
01 |
//ThreeDirectionController.h文件 |
02 |
03 |
#ifndef __THREE_DIRECTION_CONTROLLER_H__ |
04 |
#define __THREE_DIRECTION_CONTROLLER_H__ |
05 |
06 |
#include "Controller.h" |
07 |
#include "cocos2d.h" |
08 |
09 |
using namespace cocos2d; |
10 |
11 |
class ThreeDirectionController : public Controller { |
12 |
public : |
13 |
CREATE_FUNC(ThreeDirectionController); |
14 |
virtual bool init(); |
15 |
virtual void update( float dt); |
16 |
17 |
/* 触屏事件 */ |
18 |
virtual void registerWithTouchDispatcher(); |
19 |
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); |
20 |
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); |
21 |
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); |
22 |
23 |
/* 设置X方向的移动速度 */ |
24 |
void setiXSpeed( int iSpeed); |
25 |
26 |
/* 设置Y方向的移动速度*/ |
27 |
void setiYSpeed( int iSpeed); |
28 |
29 |
private : |
30 |
int iXSpeed; |
31 |
int iYSpeed; |
32 |
}; |
33 |
34 |
#endif |
这个是头文件,我们来看看主要有什么东西。基本上和SimpleMoveController差不多,区分了X和Y方向上的速度,以及加入了触屏事件(继承自CCLayer)。
再来看看cpp文件:
01 |
#include "ThreeDirectionController.h" |
02 |
03 |
bool ThreeDirectionController::init() |
04 |
{ |
05 |
this ->iXSpeed = 0; |
06 |
this ->iYSpeed = 0; |
07 |
08 |
/* 设置允许触屏 */ |
09 |
this ->setTouchEnabled( true ); |
10 |
11 |
this ->scheduleUpdate(); |
12 |
return true ; |
13 |
} |
14 |
15 |
void ThreeDirectionController::update( float dt ) |
16 |
{ |
17 |
if (mControllerListener == NULL) { |
18 |
return ; |
19 |
} |
20 |
21 |
CCPoint curPos = mControllerListener->getCurPosition(); |
22 |
curPos.x += iXSpeed; |
23 |
curPos.y += iYSpeed; |
24 |
25 |
mControllerListener->setSimplePosition(curPos.x + iXSpeed, curPos.y + iYSpeed); |
26 |
} |
27 |
28 |
void ThreeDirectionController::setiXSpeed( int iSpeed ) |
29 |
{ |
30 |
this ->iXSpeed = iSpeed; |
31 |
} |
32 |
33 |
void ThreeDirectionController::setiYSpeed( int iSpeed ) |
34 |
{ |
35 |
this ->iYSpeed = iSpeed; |
36 |
} |
37 |
38 |
bool ThreeDirectionController::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent ) |
39 |
{ |
40 |
return true ; |
41 |
} |
42 |
43 |
void ThreeDirectionController::ccTouchMoved( CCTouch *pTouch, CCEvent *pEvent ) |
44 |
{ |
45 |
/* 获取点击的坐标 */ |
46 |
CCPoint touchLocation = pTouch->getLocationInView(); |
47 |
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation); |
48 |
49 |
/* 被控制对象的坐标 */ |
50 |
CCPoint pos = mControllerListener->getCurPosition(); |
51 |
52 |
/* 判断是向上移动还是向下移动 */ |
53 |
int iSpeed = 0; |
54 |
if (touchLocation.y > pos.y) { |
55 |
iSpeed = 1; |
56 |
} |
57 |
else { |
58 |
iSpeed = -1; |
59 |
} |
60 |
61 |
setiYSpeed(iSpeed); |
62 |
} |
63 |
64 |
void ThreeDirectionController::ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent ) |
65 |
{ |
66 |
/* 停止Y坐标上的移动 */ |
67 |
setiYSpeed(0); |
68 |
} |
69 |
70 |
void ThreeDirectionController::registerWithTouchDispatcher() |
71 |
{ |
72 |
/* 注册触屏事件 */ |
73 |
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate( this , 0, true ); |
74 |
} |
首先,update函数变了一点,x和y方向上的坐标都会改变。
然后有个registerWithTouchDispatcher函数,是用于注册触屏事件的。
再然后最重要的是ccTouchMoved函数,根据点击屏幕的y坐标是在主角之上还是下来判断是让主角向上移动还是向下移动。
好了,现在!打开我们的TollgateScene.cpp的init函数,将SimpleMoveController替换为ThreeDirectionController:
1 |
/* ------------ 创建玩家移动控制器 -------------- */ |
2 |
ThreeDirectionController* mSMoveControll = ThreeDirectionController::create(); |
3 |
mSMoveControll->setiXSpeed(1); |
4 |
mSMoveControll->setiYSpeed(0); |
5 |
6 |
/* 控制器要添加到场景中才能获得update事件 */ |
7 |
this ->addChild(mSMoveControll); |
8 |
9 |
mPlayer->setController(mSMoveControll); |
Ok~运行游戏,成功了~好有意思的游戏,噗~
本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366381,如需转载请自行联系原作者