You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript开发自选冒险游戏KRATOS角色无响应问题求助

问题原因
  • 核心报错来自全局作用域下的无效变量引用:game_2、game_2_1、game_2_2、game_3等变量均是各函数内部的局部变量,仅能在函数内部访问,全局直接判断这些变量会抛出ReferenceError异常,直接终止代码运行,导致后续if (game == "KRATOS")的逻辑永远不会被执行。
  • 额外的小问题:SORA_TRAIN函数中if (game_2_1 == "GOOFY");末尾多了一个多余分号,会导致无论用户输入什么,后续SORA_GOOFY()的逻辑都会强制执行。
  • 所有多余的全局分支判断完全没有必要:每个分支的判断逻辑已经在对应的函数内部实现了,外层不需要重复编写。
修复代码

你只需删除所有多余的全局if判断、去掉多余的分号即可,修复后的完整JS代码如下:

function startGame() {
  var game = prompt("Choose your character (KRATOS/SORA)");
  document.getElementById("answer").innerHTML = game;


  function SORA() {
    var game_s = prompt("You have chosen SORA! The wielder of the Keyblade! You must fight the mighty KRATOS! The god of war! How will you prepare? (TRAIN/ADVENTURE)");
    if (game_s == "TRAIN") {
      SORA_TRAIN();
    }

    if (game_s == "ADVENTURE") {
      SORA_ADVENTURE();
    }
  }

  function SORA_TRAIN() {
    var game_2_1 = prompt("You have chosen TRAIN! You train with your friends Donald and Goofy! You have become stronger in the ways of combat! The battle draws near! You feel prepared and confident for the fight. You may take one partner with you. Who do you bring? (DONALD/GOOFY) ");
    if (game_2_1 == "DONALD") {
      SORA_DONALD();
    }
    if (game_2_1 == "GOOFY") {
      SORA_GOOFY();
    }

  }

  function SORA_DONALD() {
    alert("You have brought Donald! A powerful magician! He and you face Kratos on the battlefield. Donald casts an invincibility spell on you and you easily defeat Kratos! The end.");
  }

  function SORA_GOOFY() {
    alert("You have brought Goofy! A powerful warrior! He and you face Kratos on the battlefield. Goofy gets knocked out by Kratos in one blow! However, your training has kept you nimble! You dodge Kratos and all his attacks! You defeat Kratos! The end.");
  }

  function SORA_ADVENTURE() {
    var game_2_2 = prompt("You have chosen ADVENTURE! You adventure with your friends Mickey and Minnie! Through your exploration you have become stronger and wiser! You feel prepared and confident for the fight! You may take one partner with you. Who do you bring? (MICKEY/MINNIE)");

    if (game_2_2 == "MICKEY") {
      SORA_MICKEY();
    }
    if (game_2_2 == "MINNIE") {
      SORA_MINNIE();
    }


  }

  function SORA_MICKEY() {
    alert("You have brought Mickey! The King and fellow keyblade warrior! He and you face Kratos on the battlefield. Mickey and you rush to face Kratos and you easily overpower Kratos! The end.");
  }

  function SORA_MINNIE() {
    alert("You have brought Minnie! The powerful Queen and magician! She and you face Kratos on the battlefield. Minnie casts an invincibility spell on you and you easily defeat Kratos! The end.");
  }


  function KRATOS_1() {
    var game_3 = prompt("You have chosen KRATOS! The god of war! You must fight Sora! The legendary Keyblade wielder! How will you prepare? (HUNT/WAIT)");

    if (game_3 == "HUNT") {
      KRATOS_HUNT();
    }
    if (game_3 == "WAIT") {
      KRATOS_WAIT();
    }
  }

  function KRATOS_WAIT() {
    var game_4_1 = prompt("You have chosen WAIT! As the god of war you fear no mortal! The fight draws near. What weapon will you take into battle? (BOW/HAMMER)")

    if (game_4_1 == "BOW") {
      KRATOS_BOW();
    }

    if (game_4_1 == "HAMMER") {
      KRATOS_HAMMER();
    }

  }

  function KRATOS_BOW() {
    alert("You have chosen BOW! You bring your BOW to the battlefield. You pull back your string and let your arrow fly! You knock SORA's blade from his hand and he is unable to fight! You win! The end.")
  }

  function KRATOS_HAMMER() {
    alert("You have chosen HAMMER! You bring your HAMMER to the battlefield. You swing back your HAMMER and slam it to the ground making a tremendous earthquake. Your opponent cannot stand up! You win! The end.")
  }

  function KRATOS_HUNT() {
    var game_3_1 = prompt("You have chosen HUNT! You venture out into the forests to fight ferocious beasts to prepare! You feel that you have improved your strength. The battle draws near! Which weapon will you take to battle? (AXE/BLADES) ")

    if (game_3_1 == "AXE") {
      KRATOS_AXE();
    }

    if (game_3_1 == "BLADES") {
      KRATOS_BLADES();
    }

  }

  function KRATOS_AXE() {
    alert("You have chosen AXE! You enter the battlefield wielding your mighty AXE forged by the norse gods! You throw your weapon and are able to knock SORA's Keyblade from his grip! With no way for your opponent to fight. You win! The end.")
  }

  function KRATOS_BLADES() {
    alert("You have chosen BLADES! You enter the battlefield wielding your mighty BLADES forged by the greek gods! You swing at SORA but he is too quick! He is able to get behind you and knock you unconscious! You lose! The end. ")
  }

  // 仅保留最外层的角色判断即可
  if (game == "SORA") {
    SORA();
  }
  if (game == "KRATOS") {
    KRATOS_1()
  }
}

HTML代码不需要修改。

内容的提问来源于stack exchange,提问作者David Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:45:10