设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

使用AngularJS开发2048游戏

2014-10-13 11:29| 发布者: joejoe0332| 查看: 2858| 评论: 0|原作者: Jiango, 徐继开, LeoXu, 月亮船, 小檀, 我不叫大脸猫, 成熟的毛毛虫, uian2000, 0x0bject|来自: oschina

摘要: 我频繁地被问及到的一个问题之一,就是什么时候使用Angular框架是一个糟糕的选择。我的默认答复是编写游戏的时候,尽管Angular有它自己的 事件循环处理 ($digest循环) ,并且游戏通常需要很多底层DOM操作.如果说有An ...


模块结构

  布局angular应用使用的结构现在是根据函数推荐的,而不是类型。这就是说,不用把组件分成控制器,服务,指令等,就可以在函数基础上定义我们的模块结构。例如,在应用中定义一个Game模块和一个Keyboard模块。


  模块结构清晰地为我们分离出匹配文件结构的职能域。这不仅方便我们创建大型,灵活性强的angular应用,也方便我们共享app中的函数。


  最后,我们搭建测试环境适应文件目录结构。


视图

  应用中最易切入的地方非视图莫属了。审视视图自身,我们发现只有一个view/template.在这个应用中,不需要多视图,所以我们创建单一的<div>元素,用来放置应用的内容。


  在我们的主文件app/index.html中,我们需要包含所有的依赖项(包括angular.js自身和JS文件,即scripts/app.js),如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- index.html -->
<doctype html>
<html>
  <head>
    <title>2048</title>
    <link rel="stylesheet" href="styles/main.css">
  </head>
  <body ng-app="twentyfourtyeightApp"
    <!-- header -->
    <div class="container" ng-include="'views/main.html'"></div>
    <!-- script tags -->
    <script src="bower_components/angular/angular.js"></script>
    <script src="scripts/app.js"></script>
  </body>
</html>

Feel free to make a more complex version of the game with multiple views – please leave a comment below if you do. We’d love to see what you create.


  有了app/index.html文件集,我们需要在应用视图层面上,详细地处理app/views/main.html中的视图。当需要在应用中导入一个新
资源时,我们需要修改index.html文件。


  打开app/views/main.html,我们要替换所有的游戏指定的视图。使用controllerAs语法,我们可以在$scope中清楚地知道我们期待在哪里查询数据,哪个控制器负责哪个组件。
1
2
3
4
<!-- app/views/main.html -->
<div id="content" ng-controller='GameController as ctrl'>
  <!-- Now the variable: ctrl refers to the GameController -->
</div>

ThecontrollerAssyntax is a relatively new syntax that comes with version 1.2. It is useful when dealing with many controllers on the page as it allows us to be specific about the controllers where we expect functions and data to be defined.


  在视图中,我们要显示以下一些项目:

  1. 游戏静态头

  2. 当前游戏分数和本地用户最高分

  3. 游戏板


  游戏静态头可以这样来完成:

1
2
3
4
5
6
7
8
9
10
11
<!-- heading inside app/views/main.html -->
<div id="content" ng-controller='GameController as ctrl'>
  <div id="heading" class="row">
    <h1 class="title">ng-2048</h1>
    <div class="scores-container">
      <div class="score-container">{{ ctrl.game.currentScore }}</div>
      <div class="best-container">{{ ctrl.game.highScore }}</div>
    </div>
  </div>
  <!-- ... -->
</div>

  注意到,当在视图中引用currentScore和highScroe时,我们也引用了GameController.controllerAs语法使得我们可以显示地引用我们感兴趣的控制器。


GameController

  现在我们有了一个合理的项目结构,现在来创建GameController来放置我们要在视图中显示的值。在app/script/app.js中,我们可以在主模块twentyfourtyeight.App中创建控制器:

1
2
3
4
angular
.module('twentyfourtyeightApp', [])
.controller('GameController'function() {
});


  在视图中,我们引用了一个game对象,它将在GameController中设置。该game对象将引用主game对象。我们在一个新模块中创建这个主游戏模块,用来放置游戏中所有的引用。

  

  因为这个模块还没有创建,app不会再浏览器中加载它。在控制器中,我们可以添加GameManager依赖


1
2
3
.controller('GameController'function(GameManager) {
  this.game = GameManager;
});


  别忘了,我们正创建一个模块级别的依赖,它是应用中不同的部分,所以要确保它在应用中正确地加载,我们需要将它列为angular模块的一个依赖。为使Game模块成为twentyfourtyeightApp的依赖,我们在定义该模块的数组中列举它。


  我们整个的app/script/app.js文件应该看起来像这样:

1
2
3
4
5
angular
.module('twentyfourtyeightApp', ['Game'])
.controller('GameController'function(GameManager) {
  this.game = GameManager;
})

Game

  既然我们有视图间部分相互连接了,那么就可以开始编写游戏背后的逻辑了。为创建一个新游戏模块,我们在app/scripts/目录中把我们的模块创建为app/scripts/game/game.js:

1
angular.module('Game', []);

When building modules, we like to write them in their own directory named after the module. We’ll implement the module initialization in a file by the name of the module. For instance, we’re building a game module, so we’ll build our game module inside theapp/scripts/gamedirectory in a file namedgame.js. This methodology has provided to be scalable and logical in production.


  Game模块将提供一个单核心组件:GameManager.


  我们将来完成GameManager,使它能处理游戏的状态,用户可以移动的不同方法,记录分数以及决定游戏何时结束和用户是否打破最高分以及用户是否输局了。


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部