技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> JavaScript --> 使用JavaScript和Canvas开发游戏(三)

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

浏览:2086次  出处信息
>
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. 《部落冲突》的设计    (阅读:3161)
  2. Canvas学习教程 : Canvas介绍    (阅读:3103)
  3. 使用canvas绘制时钟    (阅读:2806)
  4. 手机游戏设计初体验    (阅读:2761)
  5. 游戏多服务器架构的一点想法    (阅读:2739)
  6. 使用JavaScript和Canvas开发游戏    (阅读:2572)
  7. HTML5 Canvas(画布)教程    (阅读:2544)
  8. 游戏程序守护进程-Windows版    (阅读:2411)
  9. 游戏动作感设计初探    (阅读:2239)
  10. 使用JavaScript和Canvas开发游戏(一)    (阅读:2241)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:使用JavaScript和Canvas开发游戏(二)
后一篇:使用JavaScript和Canvas开发游戏(四) >>
文章信息
建议继续学习
近3天十大热文
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1