我不断收到与此代码的各种错误。它应该是Gamebook引擎代码。一个简单的。我对此有种种错误的认识,我也不知道为什么。

我的控制台一直在说未找到第1行()未定义404。

其次,我所有的内容都始终在页面底部加载。

第三...图像应该在屏幕上加载,但不是。如果添加了电影,它将播放电影,但不显示图像。

你能告诉我我做错了吗?

编辑:固定代码来表示解决方案:



<html>
<style>
  objdc {
    cursor: pointer;
    text-decoration: underline;
    color: blue;
  }
</style>



<body>
  <div>
    <div id="StartRoomLoad" name="StartRoomLoad"></div>
    <div id="StartRoomText"></div>
  </div>
</body>



<script type="text/javascript" src="gamebook.js"></script>
<script>
  var GameObjects = {
    'titlescreen': [
      ['RoomName', 'Title Screen'],
      ['RoomDesc', 'Your first room Desc. Go to <objdc id="room2">Room 2</objdc>'],
      ['XRes', '320'],
      ['YRes', '240'],
      ['RmImg', 'http://selmiak.bplaced.net/games/c64/zak/room/74_intro_00_256.png'],
      ['RmMov', '']
    ],
    'room2': [
      ['RoomName', 'Title Screen'],
      ['RoomDesc', 'Your first non-title screen room. Go back to the <objdc id="titlescreen">title screen room</objdc>'],
      ['XRes', '320'],
      ['YRes', '240'],
      ['RmImg', 'http://selmiak.bplaced.net/games/c64/zak/room/74_intro_00_256.png'],
      ['RmMov', '']
    ]
  }
  var GAMENAME = '';
  var OBJECTGLOBAL = ''; //Indicates the current OBJECT loaded.
  var GAMECURPLAYER = ''; //Indicates the current player you control.
  var GAMESCORE = '0';
  var GLOBALSETCLS = false;

  const OBJECTNAME = 0;
  const OBJECTDESC = 1;
  const OBJECTXSIZE = 2;
  const OBJECTYSIZE = 3;
  const OBJECTIMAGE = 4;
  const OBJECTMOVIE = 5;

  function replaceAll(str, find, replace) {
    return str.replace(new RegExp(find, 'g'), replace);
  } //end function ParsePlayerInput

  function LoadRoom(roomname) {
    //Loads the specified room and sets the global setting. If a movie file is specified, the engine will play the movie file first. Once the movie is done, it will load the room image. Currently, the video will only play thru once. Once it is played, it will just show an image. If no movie is specified, it will load simply the room background image. If no image is present, it will show no image.

    OBJECTGLOBAL = roomname;

    if (GameObjects[OBJECTGLOBAL][OBJECTMOVIE][1] != '') {

      document.getElementById("StartRoomLoad").innerHTML = '<video id="mainvid" onerror="hidevideo(OBJECTGLOBAL);" onended="hidevideo(OBJECTGLOBAL);" width="100%" height="" autoplay>' + '<source src="' + GameObjects[OBJECTGLOBAL][OBJECTMOVIE][1] + '" type="video/mp4"></video>';
    } else if (GameObjects[OBJECTGLOBAL][OBJECTIMAGE][1] != '') {
      //console.log(GameObjects[OBJECTGLOBAL][OBJECTIMAGE][1]);
      document.getElementById("StartRoomLoad").innerHTML = "<img src='" + GameObjects[OBJECTGLOBAL][OBJECTIMAGE][1] + "' id='RoomBackground' width='" + GameObjects[OBJECTGLOBAL][OBJECTXSIZE][1] + "' height='" + GameObjects[OBJECTGLOBAL][OBJECTYSIZE][1] + "'></image>";
    }
    document.getElementById("StartRoomText").innerHTML = GameObjects[OBJECTGLOBAL][OBJECTDESC][1];
  }

  function hidevideo(roomname) {
    //This function is called once a video is played on room enter. It will close the video and then show the image if there is one.

    var x = document.getElementById("mainvid");
    var y = document.getElementById("RoomBackground");

    x.style.display = "none";

    if (GameObjects[OBJECTGLOBAL][OBJECTIMAGE][1] != '') {
      document.getElementById("StartRoomLoad").innerHTML = "<img src='" + GameObjects[OBJECTGLOBAL][OBJECTIMAGE][1] + "' id='RoomBackground' width='" + GameObjects[OBJECTGLOBAL][OBJECTXSIZE][1] + "' height='" + GameObjects[OBJECTGLOBAL][OBJECTYSIZE][1] + "'></image>";
    }
  }

  LoadRoom('titlescreen');

  var spans = document.getElementsByTagName('objdc');
  for (i = 0; i < spans.length; i++)
    spans[i].onclick = doRoomLoad;

  function runSPANS() {

    var spans = document.getElementsByTagName('objdc');
    for (i = 0; i < spans.length; i++)
      spans[i].onclick = doRoomLoad;
  }

  //document.getElementsByTagName('objdc').addEventListener('click',doRoomLoad,false);

  function doRoomLoad() {
    //var room = document.getElementById(this.id);

    LoadRoom(this.id);

    runSPANS();
  }
</script>
</html>

最佳答案

它在IE而不是Chrome上运行的原因是您的if语句依赖于一个奇怪的相等边缘情况。看一下这一行:

if (GameObjects[OBJECTGLOBAL][OBJECTMOVIE][1] != ''){


GameObjects[OBJECTGLOBAL][OBJECTMOVIE]评估为数组["RmMov"]。因此,GameObjects[OBJECTGLOBAL][OBJECTMOVIE][1]undefined

undefined''进行比较是一个非常奇怪的相等检查,因此IE将整个语句解释为false,将Chrome解释为true

无论如何,假设您的数组中将有第二个值,但没有。要解决此问题,请将['RmMov',]['RmMov']更改为['RmMov', '']

那应该工作。

09-30 16:43