Chinaunix首页 | 论坛 | 博客
  • 博客访问: 126986
  • 博文数量: 16
  • 博客积分: 287
  • 博客等级: 二等列兵
  • 技术积分: 329
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-22 21:16
文章分类
文章存档

2014年(1)

2013年(6)

2012年(9)

分类: 系统运维

2012-11-02 00:21:06

创建抬头信息显示器(HUD)

抬头信息显示器(是屏幕上一块用于显示游戏相关信息的区域,这些信息可能包括玩家剩余生命条数或实时分数等。

创建HUD

调用方法,并传入一个参数specification,该参数包含了需要显示的信息及位置,调用方法如下所示:

点击(此处)折叠或打开

  1. manager.hud.setup(specification);

点击(此处)折叠或打开

  1. manager.hud.setup({
  2.     top_left: {
  3.         "Score": function() {return character.score;}
  4.     }
  5. });

作为本指南的一部分,我们将创建一个在屏幕右下角显示游戏的帧速率(FPS)HUD

点击(此处)折叠或打开

  1. manager.hud.setup({
  2.     bottom_right: {
  3.         fps : manager.getFPS
  4.     }
  5. });

更改HUD的样式及位置

页面中每一部分的样式及位置都是由CSS而非HUD对象控制的。我们可以利用如下所示的样式表格式来更改HUD所显示的信息的样式:

点击(此处)折叠或打开

  1. /* Setup the bars */

  2. #top_hud, #bottom_hud {
  3.     height:2em;
  4.     position:absolute;
  5.     left:0em;
  6.     width:100%;
  7.     background-color: black;
  8.     color: white;
  9.     background-color: rgba(0, 0, 0, 0.7);
  10. }

  11. #top_hud {
  12.     top:0em;
  13. }
  14. #bottom_hud {
  15.     bottom:0em;
  16. }

  17. /* Setup left and right part of each bar */

  18. #bottom_hud dl, #top_hud dl {
  19.     margin: 0.3em;
  20. }

  21. #bottom_hud dl.left, #top_hud dl.left {
  22.     float: left;
  23. }
  24. #bottom_hud dl.right, #top_hud dl.right {
  25.     float: right;
  26. }

  27. /* Setup labels for each item */

  28. #bottom_hud dt, #top_hud dt,
  29. #bottom_hud dd, #top_hud dd {
  30.     display: inline-block;
  31.     line-height: 1.4em;
  32.     margin: 0;
  33. }

  34. #bottom_hud dt, #top_hud dt {
  35.     padding-right: 0.5em;
  36. }

  37. #bottom_hud dd, #top_hud dd {
  38.     text-align: right;
  39. }
  40. dl.left dd {
  41.     padding-right: 1em;
  42. }
  43. dl.right dt {
  44.     padding-left: 1em;
  45. }

将上面的CSS样式拷贝到与game.js 文件在同一文件夹下的game.css 中,然后将HTML 文件中的下面这行取消注释:

点击(此处)折叠或打开

  1. <!--
  2.     Include any style sheets here
  3.     <link rel="stylesheet" type="text/css" href="gamma.css" media="all"/>
  4. -->

注意

点击查看更多其他创建HUD的方法。

完整的脚本

点击(此处)折叠或打开

  1. require([
  2.     'gma/base',
  3.     'gma/manager',
  4.     'gma/entities/character',
  5.     'gma/events',
  6.     'gma/entities/enemy',
  7.     'gma/entities/door'
  8. ],
  9.     function(gma) {
  10.         var manager = gma.manager({
  11.             width : 600,
  12.             height : 500
  13.         });
  14.         manager.hud.setup({
  15.             bottom_right: {
  16.                 fps : manager.getFPS
  17.             }
  18.         });
  19.         manager.character = gma.character({
  20.             left : 0,
  21.             bottom : 0,
  22.             width : 3,
  23.             height : 6,
  24.             depth : 3,
  25.             template : 'gorilla'
  26.         });
  27.         manager.addCustomDefinitions({
  28.             templates : {
  29.                 greencube : ['meshTemplate', {
  30.                     mesh : gma.unitCubeInfo.mesh,
  31.                     material : {color : "#090"}
  32.                 }],
  33.                 gorilla : ['colladaTemplate',
  34.                 {
  35.                     collada : {
  36.                         document : 'gorilla.dae'
  37.                     },
  38.                     yRot : 1.57,
  39.                     yOffset : -0.5,
  40.                     yScale:0.7
  41.                 }],
  42.                 brickscube : ['meshTemplate', {
  43.                     mesh : gma.unitCubeInfo.mesh,
  44.                     texture : {
  45.                         src:'bricks.jpg',
  46.                         repeatX:0.5,
  47.                         repeatY:0.5
  48.                     }
  49.                 }]
  50.             },

  51.             types : {
  52.                 jumpingJack: ['jumpingEnemy', {
  53.                     width : 1,
  54.                     height : 2,
  55.                     template : 'greencube'
  56.                 }]
  57.             }
  58.         });

  59.         var myLevel = {
  60.             spawn : {
  61.                 main : [15, 24]
  62.             },
  63.             camera : {
  64.                 attached : ['character', 0, 6, 60]
  65.             },
  66.             light : {
  67.                 myLight : {
  68.                      type : GLGE.L_POINT,
  69.                      rotY : 1.54,
  70.                      color : "#fff",
  71.                      attached : ['character', 0,5,20]
  72.                 }
  73.              },
  74.             entities : [
  75.                 gma.door({bottom:0, left:55, width:0.5, height:9, level:1}),
  76.                 {template:'brickscube', top:0, left:0, width:30, height:3},
  77.                 {template:'brickscube', top:0, left:39, width:30, height:3},
  78.                 gma.platformEnemy({bottom:0, left:45, width:3, height:6}),
  79.                 gma.patrolEnemy({bottom:0, left:6, width:3, height:6, limitLeft: 3, limitRight:12}),
  80.                 {type:'jumpingJack', bottom:0, left:21},
  81.                 {type:'jumpingJack', bottom:3, left:24},
  82.                 {type:'jumpingJack', bottom:6, left:27}
  83.             ]
  84.         };

  85.         var otherLevel = {
  86.             spawn : {
  87.                 main : [0, 0]
  88.             },
  89.             camera : {
  90.                 attached : ['character', 0, 6, 60]
  91.             },
  92.             light : {
  93.                 myLight : {
  94.                      type : GLGE.L_POINT,
  95.                      rotY : 1.54,
  96.                      color : "#fff",
  97.                      attached : ['character', 0,5,20]
  98.                 }
  99.              },
  100.             entities : [
  101.                 gma.door({bottom:0, left:25, width:0.5, height:9, level:0}),
  102.                 {template:'brickscube', top:0, left:0, width:30, height:3}
  103.             ]
  104.         };

  105.         manager.storeLevels(myLevel);
  106.         manager.storeLevels(otherLevel);

  107.         gma.keyHandler.register(37, manager.character.move.curry(gma.constants.LEFT));
  108.         gma.keyHandler.register(39, manager.character.move.curry(gma.constants.RIGHT));
  109.         gma.keyHandler.register(32, manager.character.jump);
  110.         manager.init();
  111.     }
  112. );

为游戏增色

恭喜您!距离完成本系列指南的阅读只有一步之遥!

杀死跌落的角色

到目前为止,如果角色从游戏平台中跌落的话,那么他会一直向下落,永无休止。为了防止这种情况的发生,我们可以在屏幕下方再放置一个平台,将跌落到其上的角色杀死。为此,我们可以使用Gamma 提供的方法,下面我们在主平台下方创建这个平台并将其涂成红色:

点击(此处)折叠或打开

  1. // Create a deathplatform type

  2. types : {
  3.     lava : ['deathPlatform', {template : 'redcube', depth:50}]
  4. }
  5. // Add this lava / death platform to the list of entities

  6. entities : [
  7.     {type:'lava', top:-10, left:-50, width:1000, height:50},
  8. ]

使角色复生

我们还需要将键盘上的某一按键与角色的复生关联起来(当角色死掉之后)。注册方法使其在字母r(可看作reload/restart/respawn的首字母)被按下的时候能够被调用:

点击(此处)折叠或打开

  1. gma.keyHandler.register(82, function(e) {
  2.     manager.respawn("main");
  3. });

最终完整的脚本

整个指南介绍完毕,游戏的整个脚本(game.js) 如下所示:

点击(此处)折叠或打开

  1. require([
  2.     'gma/base',
  3.     'gma/manager',
  4.     'gma/entities/character',
  5.     'gma/events',
  6.     'gma/entities/enemy',
  7.     'gma/entities/door'
  8. ],
  9.     function(gma) {
  10.         var manager = gma.manager({
  11.             width : 600,
  12.             height : 500
  13.         });
  14.         manager.character = gma.character({
  15.             left : 0,
  16.             bottom : 0,
  17.             width : 3,
  18.             height : 6,
  19.             depth : 3,
  20.             template : 'gorilla'
  21.         });
  22.         manager.addCustomDefinitions({
  23.             templates : {
  24.                 greencube : ['meshTemplate', {
  25.                     mesh : gma.unitCubeInfo.mesh,
  26.                     material : {color : "#090"}
  27.                 }],
  28.                 gorilla : ['colladaTemplate',
  29.                 {
  30.                     collada : {
  31.                         document : 'gorilla.dae'
  32.                     },
  33.                     yRot : 1.57,
  34.                     yOffset : -0.5,
  35.                     yScale:0.7
  36.                 }],
  37.                 brickscube : ['meshTemplate', {
  38.                     mesh : gma.unitCubeInfo.mesh,
  39.                     texture : {
  40.                         src:'bricks.jpg',
  41.                         repeatX:0.5,
  42.                         repeatY:0.5
  43.                     }
  44.                 }]
  45.             },

  46.             types : {
  47.                 jumpingJack: ['jumpingEnemy', {
  48.                     width : 1,
  49.                     height : 2,
  50.                     template : 'greencube'
  51.                 }],
  52.                 lava : ['deathPlatform', {template : 'redcube', depth:50}]
  53.             }

  54.         });

  55.         manager.hud.setup({
  56.             bottom_right: {
  57.                 fps : manager.getFPS
  58.             }
  59.         });


  60.         var myLevel = {
  61.             spawn : {
  62.                 main : [15, 24]
  63.             },
  64.             camera : {
  65.                 attached : ['character', 0, 6, 60]
  66.             },
  67.             light : {
  68.                 myLight : {
  69.                      type : GLGE.L_POINT,
  70.                      rotY : 1.54,
  71.                      color : "#fff",
  72.                      attached : ['character', 0,5,20]
  73.                 }
  74.              },
  75.             entities : [
  76.                 gma.door({bottom:0, left:55, width:0.5, height:9, level:1}),
  77.                 {template:'brickscube', top:0, left:0, width:30, height:3},
  78.                 {template:'brickscube', top:0, left:39, width:30, height:3},
  79.                 {type:'deathPlatform', top:-10, left:-50, width:1000, height:50, depth:50},
  80.                 gma.platformEnemy({bottom:0, left:45, width:3, height:6}),
  81.                 gma.patrolEnemy({bottom:0, left:6, width:3, height:6, limitLeft: 3, limitRight:12}),
  82.                 {type:'jumpingJack', bottom:0, left:21},
  83.                 {type:'jumpingJack', bottom:3, left:24},
  84.                 {type:'jumpingJack', bottom:6, left:27}
  85.             ]
  86.         };

  87.         var otherLevel = {
  88.             spawn : {
  89.                 main : [0, 0]
  90.             },
  91.             camera : {
  92.                 attached : ['character', 0, 6, 60]
  93.             },
  94.             light : {
  95.                 myLight : {
  96.                      type : GLGE.L_POINT,
  97.                      rotY : 1.54,
  98.                      color : "#fff",
  99.                      attached : ['character', 0,5,20]
  100.                 }
  101.              },
  102.             entities : [
  103.                 gma.door({bottom:0, left:25, width:0.5, height:9, level:0}),
  104.                 {template:'brickscube', top:0, left:0, width:30, height:3},
  105.                 {type:'deathPlatform', top:-10, left:-50, width:1000, height:50, depth:50}
  106.             ]
  107.         };

  108.         manager.storeLevels(myLevel);
  109.         manager.storeLevels(otherLevel);

  110.         gma.keyHandler.register(37, manager.character.move.curry(gma.constants.LEFT));
  111.         gma.keyHandler.register(39, manager.character.move.curry(gma.constants.RIGHT));
  112.         gma.keyHandler.register(32, manager.character.jump);
  113.         gma.keyHandler.register(82, function(e) { manager.respawn("main"); });
  114.         manager.init();
  115.     }
  116. );

下一步该做什么呢?

我们建议您试着开发更大一点儿的更富趣味性的游戏,同时可以阅读下面的内容:;如果您想开发更加专业一点的商业游戏的话,请通读如下内容:

**********************************************************************************

译后感想:其实介绍的内容本身难度并不大,照着一步步做下来再加上提供的脚本源码就很容易了;所以我们也看到了,学习使用一个新的库并没有想象中那么难,外表花哨的技术内在可能很简单,关键是我们要静下心来拿出时间去研究,当然以优秀的书籍或文章作为指引也是必不可少的。

阅读(1819) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~