2

更新メソッドを持つエンティティがゲームにあります。最も近いゾンビをターゲットにする必要があります。現在、ゾンビのリストはアクセスするグローバルオブジェクトにすぎませんが、これは間違っているようです。リストを更新メソッドに渡すことができますが、これが最善のアプローチかどうかわかりませんか?

これが私の更新方法の簡略版です:

this.update = function () {
                var targetedZombie = null;
                //TODO: should not be using the zombies object - tight coupling should be removed
                var alivezombies = [];
                for (var zombie in zombies) {
                    if (zombies[zombie].Alive) {
                        alivezombies.push(zombies[zombie]);
                    }
                }

                targetedZombie = this.GetClosestEntity(alivezombies);
                if (targetedZombie) {
                    Fire(this, targetedZombie);
                }
});
4

2 に答える 2

1

クロージャを使用する //API を初期化する

    (function(global) {
      var zombies = []; // accessible only to you

      function zombieManager() { 
         this.addZombie = function() {  zombies.push() }
         this.killHalfZombies = function() { 
                  zombies = zombies.filter(function(item,i) { return i % 2 == 0});
         }
      }
      global.zombieManager = new zombieManager();

      function hero() {

      };

      hero.prototype.update = function() {
        //dig zombies[]
        //do whatever state change
      };
      global.hero = hero;
    })(window); //<-- you pass in whatever rootlevel object we have. window in a browser.

//use api
    var myhero = new hero();
    hero.update() //<-- this can access zombies

    zombieManager.addZombie(); //<-- zombieManager is a singleton and is responsible for zombification
于 2012-04-23T20:32:26.547 に答える
1

「ゲーム アーキテクチャについて学習するための優れたリソース」は、ゲーム アーキテクチャに関する優れたリソースです。ゲーム開発で。一般に、ゲームのコンポーネントを 1 つの概念に分割することをお勧めします。この単純なシナリオでは、ゾンビのリストを Game クラスのコンポーネントにし、メソッドを FindClosest(PositionOfHero) にします。ウィンドウは正しいオブジェクト グラフのみを開始し、グラフ間のリンクをグローバル配列として保持しませんでした。

于 2012-04-23T20:40:08.563 に答える