我现在是一名高中生,正在努力学习如何编程,但是我对目前遇到的这个问题感到十分畏惧。
我正在尝试使用rot.js libraries编写一个小游戏。具体来说,我现在正在尝试为某些项目创建一个随机名称生成器。为此,我尝试创建多个不同单词的数组,我可以随机选择数组的一个成员,并将其与其他数组中其他随机选择的单词拼接在一起,以创建一些模糊的roguelike-y名称。
Rot.js comes with a handy tool为您选择数组的随机成员。但是每当我尝试使用它时,JavaScript控制台都会向我吐出“ReferenceError:未定义的剑”。不幸的是,我有99.99%的信心,我已经宣布我的阵列'剑'为全球,所以我完全感到困惑。
我已经google了一个答案,虽然提供的解决方案要么无效,要么超出我目前对javascript的理解。
我的数组如下所示,名为words.js:
var swords = new Array();
var swords = ["sabre", "scimitar", "sword", "blade", "katana", "claymore", "gladius", "spatha", "broadsword", "jian", "talwar", "kris", "rapier"];
我试图在一个名为phraseGenerator.js的文件中随机选择其中一个单词的函数如下所示
var weaponGenerate = function() {
return swords.random(); //swords from swords.js; random() from the ROT.js api
}
console.log(this.weaponGenerate());
最后,我试图用来运行所有内容的html文件如下所示,在一个名为phraseGenerator.html的文件中
<!DOCTYPE html>
<html>
<body>
<script src="phraseGenerator.js" type="text/javascript"></script>
<script src="words.js" type="text/javascript"></script>
<script src="bower_components/rot.js/rot.js" type="text/javascript"></script>
</body>
</html>
任何帮助或形式的解释将不胜感激!谢谢。
答案 0 :(得分:1)
words.js - 重新创建数组是多余的
//removed var swords = new Array(); as you are already creating brand new array below
var swords = ["sabre", "scimitar", "sword", "blade", "katana", "claymore", "gladius", "spatha", "broadsword", "jian", "talwar", "kris", "rapier"];
phraseGenerator.js - 删除this
关键字,因为它未链接到任何对象/功能/环境
var weaponGenerate = function() {
return swords.random(); //swords from swords.js; random() from the ROT.js api
}
console.log(weaponGenerate()); //removed `this` keyword
index.html - 顺序非常重要,首先是rot.js
,然后是words.js
,最后是phaseGenerator.js
,因为rot.js
必须延伸< strong>数组的功能,以便在生成器中,数组具有random()
方法
<!DOCTYPE html>
<html>
<body>
<script src="bower_components/rot.js/rot.js" type="text/javascript"></script>
<script src="words.js" type="text/javascript"></script>
<script src="phraseGenerator.js" type="text/javascript"></script>
</body>
</html>
<script src="https://rawgit.com/ondras/rot.js/master/rot.js"></script>
<script>
//removed var swords = new Array(); as you are already creating brand new array below
var swords = ["sabre", "scimitar", "sword", "blade", "katana", "claymore", "gladius", "spatha", "broadsword", "jian", "talwar", "kris", "rapier"];
</script>
<script>
var weaponGenerate = function() {
return swords.random(); //swords from swords.js; random() from the ROT.js api
}
console.log(weaponGenerate()); //removed `this` keyword
</script>
<button id="button">generated word is <b id="word">EMPTY</b> <em>Click to generate more</em></button>
<script>
//for demo purpose
document.querySelector('#button').onclick = function(){
document.querySelector('#word').innerHTML = weaponGenerate();
}
button
</script>
&#13;
答案 1 :(得分:0)
<!DOCTYPE html>
<html>
<body>
<script src="bower_components/rot.js/rot.js" type="text/javascript"></script>
<script src="words.js" type="text/javascript"></script>
<script src="phraseGenerator.js" type="text/javascript"></script>
</body>
</html>
答案 2 :(得分:0)
主要问题是脚本的顺序。首先调用rot.js。是的,var sword = new Array
是不必要的,尽管你不会收到错误。
<!DOCTYPE html>
<html>
<body>
<script src="bower_components/rot.js/rot.js" type="text/javascript"></script>
<script src="words.js" type="text/javascript"></script>
<script src="phraseGenerator.js" type="text/javascript"></script>
</body>
</html>