IT技术博客大学习 共学习 共进步

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

为之漫笔 2011-08-21 10:25:18 浏览 2,641 次
>
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. 《部落冲突》的设计 (阅读 4,902)
  2. Canvas学习教程 : Canvas介绍 (阅读 4,201)
  3. 手机游戏设计初体验 (阅读 4,103)
  4. 游戏多服务器架构的一点想法 (阅读 3,742)
  5. 使用canvas绘制时钟 (阅读 3,601)
  6. HTML5 Canvas(画布)教程 (阅读 3,541)
  7. 使用JavaScript和Canvas开发游戏 (阅读 3,521)
  8. 游戏程序守护进程-Windows版 (阅读 3,444)
  9. 游戏动作感设计初探 (阅读 3,421)
  10. 网络游戏的社会化 (阅读 3,363)