IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

使用JavaScript和Canvas开发游戏(三)

为之漫笔 2011-08-21 10:25:18 累计浏览 2,684 次
本机暂存
>
2 this.context2D.drawImage(this.backBuffer, 0, 0);

理解了draw函数,下面再分别讲一讲addGameObject和removeGameObject函数。


/**
    向gameObjects集合中添加一个GameObject
    @param gameObject The object to add
*/
this.addGameObject = function(gameObject)
{
    this.gameObjects.push(gameObject);
    this.gameObjects.sort(function(a,b){return a.zOrder - b.zOrder;})
};
/**
    从gameObjects集合中删除一个GameObject
    @param gameObject The object to remove
*/
this.removeGameObject = function(gameObject)
{
    this.gameObjects.removeObject(gameObject);
}
01 /**
02     向gameObjects集合中添加一个GameObject
03     @param gameObject The object to add
04 */
05 this.addGameObject = function(gameObject)
06 {
07     this.gameObjects.push(gameObject);
08     this.gameObjects.sort(function(a,b){return a.zOrder - b.zOrder;})
09 };
10 /**
11     从gameObjects集合中删除一个GameObject
12     @param gameObject The object to remove
13 */
14 this.removeGameObject = function(gameObject)
15 {
16     this.gameObjects.removeObject(gameObject);
17 }

利用addGameObject和removeGameObject(在Utils.js文件里通过扩展Array.prototype添加)函数,可以在GameObjectManager所维护的GameObject集合(即gameObjects变量)中添加和删除游戏对象。

GameObjectManager类是我们这个游戏框架中最复杂的一个类。在下一篇文章中,我们会讲解游戏框架的另外几个类:GameObject、VisualGameObject、Bounce和ApplicationManager。

好了,现在放松一下,看一看Demo吧

同分类推荐文章

  1. translateZ() (2026-06-25 21:18:56)
  2. translateY() (2026-06-25 21:17:56)
  3. translateX() (2026-06-25 21:16:01)

查看更多 前端 文章 →

建议继续学习

  1. 我是如何学习计算机编程的 (累计阅读 181,142)
  2. JQuery实现Excel表格呈现 (累计阅读 48,349)
  3. 深入理解Javascript之执行上下文(Execution Context) (累计阅读 18,403)
  4. HTML5 离线缓存-manifest简介 (累计阅读 17,102)
  5. 从输入 URL 到页面加载完成的过程中都发生了什么事情? (累计阅读 15,932)
  6. 图片动态局部毛玻璃模糊效果的实现 (累计阅读 14,848)
  7. 天朝第二代身份证号码的验证机制 (累计阅读 14,761)
  8. HTML 5 的data-* 自定义属性 (累计阅读 14,349)
  9. 分享一个JQUERY颜色选择插件 (累计阅读 14,223)
  10. 什么是全栈工程师? (累计阅读 14,036)