WarriorJS Docs
  • Player
  • Maker
  • 社區
  • 繁體中文
    • English
    • العربية
    • Català
    • Čeština
    • Deutsch
    • Ελληνικά
    • Español
    • Français
    • Italiano
    • Polskie
    • Русский
    • Српски језик (Ћирилица)
    • Svenska
    • Türkçe
    • 中文
    • 協助翻譯
  • GitHub

›提示 & 技巧

遊戲

  • 概覽
  • 目的
  • 開始遊戲
  • 視角
  • 得分
  • 史詩模式
  • 塔樓

概念

  • 單位
  • 勇士
  • 技能
  • 空間

玩家 API

  • 空間 API
  • 單位 API
  • 回合應用程式介面 (Turn API)

提示 & 技巧

  • 一般
  • JavaScript
  • 人工智能
  • 命令列

命令列

  • Install
  • Options
Translate

JavaScript 秘訣

  • 不要純粹把大堆代碼填滿整個playTurn方法 (method),嘗試以方法及類別把它組織好。例如:
class Player {
  playTurn(warrior) {
    if (this.isInjured(warrior)) {
      warrior.rest();
    }
  }

  isInjured(warrior) {
    return warrior.health() < 20;
  }
}
  • 如果你想在每一層的開始都執行一些一樣的代碼,你可在Player類別中定義 (define) 一個建構式 (constructor),就像:
class Player {
  constructor() {
    // This code will be executed only once, at the beginning of the level.
    this.health = 20;
  }

  // ...
}
  • 在使用感官技 (sense) 後,你可以直接呼叫 (call) 空間應用程式介面 (Space API) 的方法, 例如新手塔 (Baby Steps tower) 的"感應 (feel)" 感官技就會傳回一個空間 (space), 在移動到這空間前,你可以呼叫isEmpty()來判斷這空間是否已清空。
class Player {
  playTurn(warrior) {
    if (warrior.feel().isEmpty()) {
      warrior.walk();
    }
  }
}
  • 有一些感官技 (如新手塔的 "觀察 (look)"及"聆聽 (listen)") 並不傳回空間,而是傳回一個空間陣列 (array),你可以在陣列原型方法 (Array Prototype methods)中找到很多有用的方法。 以下是一個Array.prototype.find方法的例子:
class Player {
  // ...

  isEnemyInSight(warrior) {
    const spaceWithUnit = warrior.look().find(space => space.isUnit());
    return spaceWithUnit && spaceWithUnit.getUnit().isEnemy();
  }
}
← 基本秘訣人工智能秘訣 →
WarriorJS Docs
Docs
PlayerMaker
Community
SpectrumTwitterFollow WarriorJS on Twitter
More
DonateGitHubStar