JavaScript在拆分为子函数时不工作
我用JavaScript编写了一个游戏。它很管用,但现在我正试图通过将代码拆分为更小的函数和文件,使代码更易于重用和调试。下面是在游戏循环中反复调用的play函数:JavaScript在拆分为子函数时不工作,javascript,Javascript,我用JavaScript编写了一个游戏。它很管用,但现在我正试图通过将代码拆分为更小的函数和文件,使代码更易于重用和调试。下面是在游戏循环中反复调用的play函数: function play(deltaTime) { if (gameScene.visible == true) { explorer.x += explorer.vx * deltaTime; explorer.y += explorer.vy * del
function play(deltaTime) {
if (gameScene.visible == true) {
explorer.x += explorer.vx * deltaTime;
explorer.y += explorer.vy * deltaTime;
//Contain the explorer inside the area of the dungeon
contain(explorer, {
x: 1,
y: 1,
width: canvasWidth,
height: canvasHeight
});
var explorerHit = false;
makeEnemiesMove();
//##############################################################################
//If the explorer is hit...
if (explorerHit) {
if (!damageSound.playing()) {
damageSound.play();
}
//Make the explorer semi-transparent
explorer.alpha = 0.5;
//Reduce the width of the health bar's inner rectangle by 1 pixel
healthBar.outer.width -= 1;
} else {
//Make the explorer fully opaque (non-transparent) if it hasn't been hit
explorer.alpha = 1;
}
//################################################################
//Does the explorer have enough health? If the width of the `innerBar`
//is less than zero, end the game and display "You lost!"
if (healthBar.outer.width < 0) {
gameOverSound.play();
}
//Check for a collision between the explorer and the treasure
if (hitTestRectangle(explorer, treasure)) {
//If the treasure is touching the explorer, center it over the explorer
treasure.x = explorer.x + 8;
treasure.y = explorer.y + 8;
if (carrying < 1) {
pickUpSound.play();
carrying = 1;
}
}
//If the explorer has brought the treasure to the exit,
//end the game and display "You won!"
if (hitTestRectangle(treasure, door)) {
victorySound.play();
state = end;
}
}
}
我尝试在原始文件中调用它,如下所示:
var explorerHit = false;
makeEnemiesMove();
checkForPlayerDamage();
checkForPlayerDamage()
错误消息中引用的explorerHitVariable在调用此函数之前定义,如下所示:
var explorerHit = false;
makeEnemiesMove();
checkForPlayerDamage();
索引文件中引用的相关文件如下:
var explorerHit = false;
makeEnemiesMove();
checkForPlayerDamage();
索引文件中引用了相关JavaScript文件,如下所示:
<script language="JavaScript" type="text/javascript" src="gameScene/checkForPlayerDamage.js"></script>
<script language="JavaScript" type="text/javascript" language="JavaScript" type="text/javascript" src="play.js"></script>
任何帮助都将不胜感激。explorerHit变量是在
play()
函数中声明的,因此它在该函数之外不可见。这称为局部范围。您需要将该值作为参数传递给checkForPlayerDamage()
,以便该值也可以在那里使用:
...
makeEnemiesMove();
checkForPlayerDamage(explorerHit);
...
以及拆分功能:
function checkForPlayerDamage(explorerHit) { ...
(假设checkForPlayerDamage()
中使用的所有其他变量都是全局变量。)
您可以在这里学习JavaScript中的不同作用域机制:
您的浏览器控制台中出现了哪些错误?“它不起作用”不能为我们提供任何可以用来诊断问题的信息。它怎么不起作用?你已经回答了你自己的问题。如果将注释标记之间的代码移出函数(
play
),则该代码将不再具有访问该函数局部变量的权限(explorerHit
)。您需要通过在更高的作用域中声明来访问函数外部所需的数据。了解更多有关作用域的信息,您的变量是函数的本地变量play
,如果希望其他函数访问它,请将其设置为global@Roljhon不,不要让它全球化。如果你能帮助的话,不要把任何东西全球化。只需使其具有比现在更高的作用域--所有需要访问变量的代码都将共享该作用域。@ScottMarcus,单词global
的错误选择太普遍了。应该更具体些,但无论如何,你是对的:)