2 |
this.context2D.drawImage(this.backBuffer, 0, 0); |
理解了draw函数,下面再分别讲一讲addGameObject和removeGameObject函数。
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吧。