所以我一直在构建一个有趣的基于文本的 javascript 游戏,它严重依赖 OBP,但我遇到了一个障碍。我将声明一个变量但不给它任何数据,然后我将创建一个函数,将先前创建的对象添加到它的数据中。唯一的问题是当我这样做时,如果我调用该函数,它将不允许我访问 var 的数据。有人可以告诉我这段代码有什么问题吗?非常感谢,他是我需要帮助的代码:
function Monster (name,health,attack,def,moneycarry, crit, dodge){
this.Name = name;
this.Health = health;
this.Attack = attack;
this.Def = def;
this.Gold=moneycarry;
this.CritChance = crit;
this.DodgeChance = dodge;
}
var SelectedMonster;
var Dragon = new Monster("Dragon",200,200,5,100,0.40,0,40);
var GoblinTheif = new Monster("Goblin Theif",130, 250, 0, 125,0.05,0.65);
var SkeletonT = new Monster ("Skeleton Tank", 210, 170, 30, 100, 0.3, 0.00)
function MonsterSummon(){
var MonsterSelector = Math.random();
if(MonsterSelector < 0.360 && MonsterSelector > 0){
var SelectedMonster = Dragon;
}
else if(MonsterSelector < 0.70 && MonsterSelector > 0.35){
var SelectedMonster = GoblinTheif;
}
else if(MonsterSelector >= 0.70){
var SelectedMonster = SkeletonT
}
}
MonsterSummon()
console.log(SelectedMonster.name);`
对不起,草率的格式我不知道如何让我写的代码留在stackoverflow提供的代码块中,对不起!