gpt4 book ai didi

javascript - 当另一个函数调用时,函数不会触发

转载 作者:行者123 更新时间:2023-11-28 16:12:31 24 4
gpt4 key购买 nike

温柔点,我对 JavaScript 很陌生:

我正在致力于创建一个二十一点游戏 - CodeAcademy 上的一些教程涵盖了一些基础知识,因此我尝试自己更进一步。作为打印每个玩家姓名的前身,我创建了一个函数 (showNames()) 来打印“Bang, Boom!”进入我的“HUD”部分。当我(临时)将 showNames() 分配给“Hit”按钮的 onclick 函数时,此方法有效。但是,当我尝试从 setupGame() 函数调用 showNames() 时,它不会触发。我哪里错了?代码如下:

index.html:

<!DOCTYPE html>
<html>
<head>
<title>Awesome Blackjack!</title>
<link rel="stylesheet" type="text/css" href="common/css/default.css" />
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Galindo|Duru+Sans|Jolly+Lodger|McLaren' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="common/js/blackjack_controller.js"></script>
<script type="text/javascript">
function play() {
if (setupGame()) {
showNames();
alert("Pow");
} else {
alert("You must enter a number between 1 and 4.");
}
}
</script>
</head>
<body>
<div id="wrapper">
<section id="hud">
<div class="player" id="player1">
<div class="playerName">Drew</div>
<div class="playerScore">21</div>
</div>
<div class="player">
<div class="playerName">Dalton</div>
<div class="playerScore">12</div>
</div>
<div class="player">
<div class="playerName">Lindsey</div>
<div class="playerScore">23</div>
</div>
<div class="player">
<div class="playerName">Steven</div>
<div class="playerScore">9</div>
</div>
</section>
<section id="table">
<div id="controls">
<input type="button" name="start" id="start" value="Start" class="control start" onclick="play()"; />
<input type="button" name="hit" id="hit" value="Hit" class="control hit" onclick="showNames()"; />
<input type="button" name="stay" id="stay" value="Stay" class="control stay" />
</div>
<div id="dealer">
<div id="dealerName">Dealer</div>
<div id="dealerScore">19</div>
<div id="playArea">
<div class="card dealer club first">9</div>
<div class="card dealer club">9</div>
<div class="card dealer club">9</div>
<div class="card dealer heart king"></div>
</div>
</div>
<div id="playerWrapper">
<div class="playerArea a">
<div class="playerAreaName">Drew</div>
<div class="card club first">9</div>
<div class="card club">9</div>
<div class="card club">9</div>
<div class="card heart king"></div>
</div>
<div class="playerArea b">
<div class="playerAreaName">Drew</div>
</div>
<div class="playerArea c">
<div class="playerAreaName">Drew</div>
</div>
<div class="playerArea d">
<div class="playerAreaName">Drew</div>
</div>
</div>
</section>
</div>
</body>
</html>

blackjack_controller.js:

var numberOfPlayer = 0;
var players = new Array();
var name;
var names;

// Methods
function showNames() {
document.getElementById("hud").innerHTML = "Bang, Boom!";
};

function setupGame() {
numberOfPlayers = prompt("Please enter the number of players (up to 4).");
if (numberOfPlayers > 0 && numberOfPlayers <= 4) {
for (var i = 1; i <= numberOfPlayers; i++) {
players[i] = new Player(prompt("Enter a name for player " + i + "."));
}

for (var i = 1; i <= numberOfPlayers; i++) {
names += players[i].printName();
}

return true;
}

return false;
}

// Classes
function Player(name) {
this.name = name;
this.cards = new Array();
this.score = 0;

function printName() {
return this.name + " ";
}
}

最佳答案

您定义的方法错误。它应该类似于以下内容:

this.printName = function() {
return this.name + " ";
}

DEMO

关于javascript - 当另一个函数调用时,函数不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12443208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com