09Ophone平台2D游戏引擎实现游戏框架实例.docx
- 文档编号:24451024
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:41
- 大小:213.67KB
09Ophone平台2D游戏引擎实现游戏框架实例.docx
《09Ophone平台2D游戏引擎实现游戏框架实例.docx》由会员分享,可在线阅读,更多相关《09Ophone平台2D游戏引擎实现游戏框架实例.docx(41页珍藏版)》请在冰豆网上搜索。
09Ophone平台2D游戏引擎实现游戏框架实例
通过前面八篇文章的学习,我们已经实现了一个Ophone平台的2D游戏引擎的各个模块了,我们也没有演示或者测试我们所完成的引擎是否可行,那么从这一篇文章开始,我们就通过一个简单的游戏实例来给大家演示如何使用该引擎来开发游戏,当然关于这个游戏的一些效果截图,大家可以回过去查看第一篇文章开始部分的内容。
游戏规则
在学习如何制作这个游戏之前,我们有必要对游戏的规则进行简单的介绍,要不然我们规则都不知道,又如何来做游戏呢?
当然由于时间关系,这个游戏其实也只做了一小部分,实现了一些基本的功能,这有待完善,游戏规则是:
有很多人开着一辆车去偷一家人的东西,由于车的声音比较大,只能将车停放在离房子很远的地方,然后小人要跑过去将房屋里的东西给偷出来放到车上,整个游戏场景为超过两屏的一个横版,车停在左边,房子再右边,玩家需要阻止这些人去偷东西,可以在屏幕的下方来发射不同的子弹和设置不同的陷阱来阻止小人,屏幕听不要显示时间,分数,家里的财产数,左边和右边则分别是不同类型的子弹和陷阱,小人也有5中,有跑步得,有乘滑轮的,有拉车的,拉车的一次可以拉很多东西等等,大家可以自由发挥,整个界面的布局如图9-1所示。
图9-1游戏截图
大家现在对游戏的规则应该很清楚了,下面我们就开始捉着去实现这个游戏,我们将按照游戏的执行流程来进行设计。
游戏框架
开始做一个游戏之前,一般我们会根据需求文档和策划文档来构架整个游戏框架,大家可不要小看这个游戏框架的构建,一个好的游戏框架可以为后期的开发节省很多时间,所以这里我们也将游戏框架提出来单独一节介绍,首先看一下我们所完成的整个游戏所设计的文件类结构,如图9-2所示。
图9-2文件目录结构
下面是这些文件分别对应的功能:
com.yarin.Sticker包
Constant.java:
游戏中所需要使用的一些常量,比如图层的标记等。
Sticker.java:
游戏的Activity入口。
com.yarin.Sticker.Entity包(包含游戏的所有实体)
Bullet.java:
游戏中的子弹类
Stickers.java:
游戏中的小偷类
com.yarin.Sticker.Screens包(包含游戏的所有场景)
About.java:
游戏关于界面
Help.java:
游戏帮助界面
MainMenu.java:
游戏的主菜单界面
Splash.java:
游戏的logo界面
Set.java:
游戏的设置界面
ReplaceLayer.java:
用于移动图层
ReplaceSceneLayer.java:
用于移动场景图层(特殊的图层移动)
SceneLayer.java:
游戏的场景图层
UILayer.java:
游戏的界面图层
TransitionsCamera.java:
操作摄像头相关的,该游戏中暂时没有使用
通过对该文件结构的介绍,大家应该可以猜到我们的整个游戏框架了,但是我们还是对该框架设计一个流程图,如图9-3所示。
图9-3游戏流程图
对于该游戏框架中,我们需要说明一下,在帮助,关于,设置等界面之间的切换将使用图层之间的切换ReplaceLayer,而对于主菜单和游戏中界面(游戏开始项)将使用场景层移动ReplaceSceneLayer。
该游戏中我们为整个游戏定义了一个场景,然后将通过放入不同的图层来实现不同的界面,所以这个也算是该引擎的一个小小的缺陷,我们也正好利用这个游戏的实现来学习一下图层的移动如何实现。
其实在稍后我们分析的时候,大家就会明白,其实ReplaceSceneLayer和ReplaceLayer都是在移动图层,只是ReplaceSceneLayer是比较特殊的图层移动,需要将游戏场景也一起移动,现在大家回想一下,我们的引擎中实现了场景scene之间的移动,但是没有实现图层Layer的移动,因此我们首先需要自己来实现一个图层的移动。
图层移动(ReplaceLayer)
大家都知道,我们的引擎中所定义的场景scene其实就是继承节点Node的,而我们的图层同样也是继承自节点的,所以要根据场景的移动来实现图层的移动就很简单,从前面的学习中我们知道可以使用MoveTo或者MoveBy来实现移动的动画效果,那么这里我们将使用MoveTo来给大家演示。
这里我们先看一下图层之间是如何移动的,如图9-4所示,表示正在由logo层移动到主界面层的动画。
图9-4由logo层移动到主界面层
从图中可以看到,我们需要一个图层Layer来表示当前正在进入屏幕的图层,还需要一个图层表示当前正在退出屏幕的图层,由于是动画,所以我们还需要一个用来表示执行动画的时间,同时大家应该明白我们的图层移动方式可以是从左至右,由上至下或者是方向,因此我们需要一个变量来表示移动的方向。
分析了所需要的数据,下面看看我们究竟如何实现,如代码清单9-1所示。
代码清单9-1:
ReplaceLayer
viewplaincopytoclipboardprint?
1.//移动图层
2. public class ReplaceLayer {
3. //进入屏幕的图层
4. private Layer mInLayer = null;
5. //退出屏幕的图层
6. private Layer mOutLayer = null;
7. //动画事件
8. private float mDuration = .0f;
9. //方向
10. private int mOrientation = -1;
11. //动画结束之后的回调函数
12. private Callback mCallbac = new Callback() {
13. public void onDone(Action action) {
14. //结束之后需要移除已经退出屏幕的图层
15. Director.getInstance().getRunningScene().removeChild(mOutLayer, true);
16. //激活当前进入屏幕图层的事件操作
17. mInLayer.setAccelerometerEnabled(true);
18. mInLayer.setTouchEnabled(true);
19. mInLayer.setKeyEnabled(true);
20. }
21. };
22. //构建动画
23. public static ReplaceLayer make(int orientation, float duration, Layer inLayer, Layer outLayer,int tag) {
24. return new ReplaceLayer(orientation, duration, inLayer, outLayer,tag);
25. }
26.
27. protected ReplaceLayer(int orientation, float duration, Layer inLayer, Layer outLayer,int tag) {
28. mInLayer = inLayer;
29. mOutLayer = outLayer;
30. mDuration = duration;
31. mOrientation = orientation;
32.
33. Director.getInstance().getRunningScene().addChild(mInLayer,Constant.LAYER_ORDER_Z,tag);
34. mInLayer.setPosition(Constant.SceneWidth, Constant.SceneHeight);
35.
36. mInLayer.setAccelerometerEnabled(false);
37. mInLayer.setTouchEnabled(false);
38. mInLayer.setKeyEnabled(false);
39.
40. mOutLayer.setAccelerometerEnabled(false);
41. mOutLayer.setTouchEnabled(false);
42. mOutLayer.setKeyEnabled(false);
43.
44. switch (mOrientation) {
45. case Constant.REPLACELAYER_ORIENTATION_UP:
46. replaceLayerUp();
47. break;
48. case Constant.REPLACELAYER_ORIENTATION_DOWN:
49. replaceLayerDown();
50. break;
51. case Constant.REPLACELAYER_ORIENTATION_LEFT:
52. replaceLayerLeft();
53. break;
54. case Constant.REPLACELAYER_ORIENTATION_RIGHT:
55. replaceLayerRight();
56. break;
57. }
58. }
59. //上方向
60. private void replaceLayerUp() {
61. IntervalAction outAction = MoveTo.make(mDuration, 0, 0, 0, Constant.SceneHeight);
62. mOutLayer.runAction(outAction);
63.
64. IntervalAction inAction = MoveTo.make(mDuration, 0, -Constant.SceneHeight, 0, 0);
65. inAction.setCallback(mCallbac);
66. mInLayer.runAction(inAction);
67. }
68. //下方向
69. private void replaceLayerDown() {
70. IntervalAction outAction = MoveTo.make(mDuration, 0, 0, 0, -Constant.SceneHeight);
71. mOutLayer.runAction(outAction);
72.
73. IntervalAction inAction = MoveTo.make(mDuration, 0, Constant.SceneHeight, 0, 0);
74. inAction.setCallback(mCallbac);
75. mInLayer.runAction(inAction);
76. }
77. //左方向
78. private void replaceLayerLeft() {
79. IntervalAction outAction = MoveTo.make(mDuration, 0, 0, -Constant.SceneWidth, 0);
80. mOutLayer.runAction(outAction);
81.
82. IntervalAction inAction = MoveTo.make(mDuration, Constant.SceneWidth, 0, 0, 0);
83. inAction.setCallback(mCallbac);
84. mInLayer.runAction(inAction);
85. }
86. //右方向
87. private void replaceLayerRight() {
88. IntervalAction outAction = MoveTo.make(mDuration, 0, 0, Constant.SceneWidth, 0);
89. mOutLayer.runAction(outAction);
90.
91. IntervalAction inAction = MoveTo.make(mDuration, -Constant.SceneWidth, 0, 0, 0);
92. inAction.setCallback(mCallbac);
93. mInLayer.runAction(inAction);
94. }
95.
96. }
//移动图层 publicclassReplaceLayer{ //进入屏幕的图层 privateLayermInLayer=null; //退出屏幕的图层 privateLayermOutLayer=null; //动画事件 privatefloatmDuration=.0f; //方向 privateintmOrientation=-1; //动画结束之后的回调函数 privateCallbackmCallbac=newCallback(){ publicvoidonDone(Actionaction){ //结束之后需要移除已经退出屏幕的图层 Director.getInstance().getRunningScene().removeChild(mOutLayer,true); //激活当前进入屏幕图层的事件操作 mInLayer.setAccelerometerEnabled(true); mInLayer.setTouchEnabled(true); mInLayer.setKeyEnabled(true); } }; //构建动画 publicstaticReplaceLayermake(intorientation,floatduration,LayerinLayer,LayeroutLayer,inttag){ returnnewReplaceLayer(orientation,duration,inLayer,outLayer,tag); } protectedReplaceLayer(intorientation,floatduration,LayerinLayer,LayeroutLayer,inttag){ mInLayer=inLayer; mOutLayer=outLayer; mDuration=duration; mOrientation=orientation; Director.getInstance().getRunningScene().addChild(mInLayer,Constant.LAYER_ORDER_Z,tag); mInLayer.setPosition(Constant.SceneWidth,Constant.SceneHeight); mInLayer.setAccelerometerEnabled(false); mInLayer.setTouchEnabled(false); mInLayer.setKeyEnabled(false); mOutLayer.setAccelerometerEnabled(false); mOutLayer.setTouchEnabled(false); mOutLayer.setKeyEnabled(false); switch(mOrientation){ caseConstant.REPLACELAYER_ORIENTATION_UP:
replaceLayerUp(); break; caseConstant.REPLACELAYER_ORIENTATION_DOWN:
replaceLayerDown(); break; caseConstant.REPLACELAYER_ORIENTATION_LEFT:
replaceLayerLeft(); break; caseConstant.REPLACELAYER_ORIENTATION_RIGHT:
replaceLayerRight(); break; } } //上方向 privatevoidreplaceLayerUp(){ IntervalActionoutAction=MoveTo.make(mDuration,0,0,0,Constant.SceneHeight); mOutLayer.runAction(outAction); IntervalActioninAction=MoveTo.make(mDuration,0,-Constant.SceneHeight,0,0); inAction.setCallback(mCallbac); mInLayer.runAction(inAction); } //下方向 privatevoidreplaceLayerDown(){ IntervalActionoutAction=MoveTo.make(mDuration,0,0,0,-Constant.SceneHeight); mOutLayer.runAction(outAction); IntervalActioninAction=MoveTo.make(mDuration,0,Constant.SceneHeight,0,0); inAction.setCallback(mCallbac); mInLayer.runAction(inAction); } //左方向 privatevoidreplaceLayerLeft(){ IntervalActionoutAction=MoveTo.make(mDuration,0,0,-Constant.SceneWidth,0); mOutLayer.runAction(outAction); IntervalActioninAction=MoveTo.make(mDuration,Constant.SceneWidth,0,0,0); inAction.setCallback(mCallbac); mInLayer.runAction(inAction); } //右方向 privatevoidreplaceLayerRight(){ IntervalActionoutAction=MoveTo.make(mDuration,0,0,Constant.SceneWidth,0); mOutLayer.runAction(outAction); IntervalActioninAction=MoveTo.make(mDuration,-Constant.SceneWidth,0,0,0); inAction.setCallback(mCallbac); mInLayer.runAction(inAction); } }
其中需要说明的是我们使用mDuration来表示移动的方式,具体的方向常量我们定义在了Constant.java文件中,如代码清单9-2所示。
代码清单9-2:
移动方向常量
viewplaincopytoclipboardprint?
1.//图层移动,上下左右
2. public static final int REPLACELAYER_ORIENTATION_UP = 0;
3. public static final int REPLACELAYER_ORIENTATION_DOWN = 1;
4. public static final int REPLACELAYER_ORIENTATION_LEFT = 2;
5. public static final int REPLACELAYER_ORIENTATION_RIGHT = 3;
//图层移动,上下左右 publicstaticfinalintREPLACELAYER_ORIENTATION_UP=0; publicstaticfinalintREPLACELAYER_ORIENTATION_DOWN=1; publicstaticfinalintREPLACELAYER_ORIENTATION_LEFT=2; publicstaticfinalintREPLACELAYER_ORIENTATION_RIGHT=3;
在构建动画时我们需要指定进入屏幕的图层mInLayer和退出屏幕的图层mOutLayer然后移动的四个方向我们分别通过replaceLayerUp,replaceLayerDown,replaceLayerLeft,replaceLayerRight几个函数来进行实现,下面我们选择分析其中一个向左的方法,首先构建好一个MoveTo动画,指定好移动的坐标,然后对退出屏幕的图层mOutLayer执行该动画,使其移动到屏幕以外,然后再构建另一个MoveTo动画由进入屏幕的图层mInLayer来执行,这里需要注意了,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 09 Ophone 平台 游戏 引擎 实现 框架 实例