JS互动小说

时间:2018-09-21 23:40:42

标签: javascript game-engine

我正在尝试使用JavaScript制作交互式小说游戏,我想到的第一件事就是放下导航系统,这是玩家从一个包含描述的位置(房间)移动到另一个位置的方法。我是JS的新手,现在仍然在学习,到目前为止我能编写的代码根本行不通,真的不知道为什么,有人可以帮助我吗?也许告诉我另一种更有效的方法?

node = function(desc, exits){
  this.desc = desc;
  this.exits = exits;
}

goTo = function(target){
  goTo(prompt(target[0] + ' where do you want to go? ' + target[1]))
}

/*
goTo = function(target){
  goTo(prompt(target.desc + ' where do you want to go? ' + target.exits))
}


var house = new node('this is the house desc', ['field', 'cave']);
var field = new node('this is the field desc', ['house', 'cave']);
var cave = new node('this is the cave desc', ['field', 'house']);
*/

var house = ['this is the house desc', [field, cave]]
var field = ['this is the field desc', [house, cave]]
var cave = ['this is the cave desc', [field, house]]

goTo(house);

被注释掉的那段代码是我尝试使用函数作为创建对象的一种方式来做事...不行...

1 个答案:

答案 0 :(得分:0)

更多地考虑它并在OS上搜索了一下之后,我到达了想要的地方。该代码现在可以正常工作,并且更加简单,为防万一,我将其留在这里:)

var x = 1;
var y = 0;

var o = 'Nothing here'
var house = 'House description';
var field = 'Field description';
var cave = 'Cave description';

var map = [
  [o, house, o],
  [o, field, cave],
  [o, o, o],
  [o, o, o]
];

while (y <= 3 && x <= 2 && x >= 0 && y >= 0) {
  switch (prompt(map[y][x] + ' - Your current position is: ' + 'x: ' + x + ' y: ' + y)) {
    case 'n':
      y--;
      break;
    case 's':
      y++;
      break;
    case 'e':
      x++;
      break;
    case 'w':
      x--;
      break;
    default:
      break;
  }
}