0

温柔点,我对 JavaScript 很陌生:

我正在创建一个二十一点游戏——CodeAcademy 上的一些教程涵盖了一些基础知识,所以我试图自己更进一步。作为打印每个玩家姓名的前奏,我创建了一个函数 ( showNames()) 来打印“Bang, Boom!” 进入我的“hud”部分。当我(临时)分配showNames()onclick我的“点击”按钮的功能时,这有效。但是,当我尝试showNames()从我的setupGame()函数调用时,它不会触发。我哪里错了?下面的代码:

索引.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 + " ";
    }
}
4

2 回答 2

3

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

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

演示

于 2012-09-15T23:28:20.560 回答
1

Player.printName 只存在于函数 Player 中。您需要返回(引用)它才能从外部访问它。

此外..Player可以以不同的方式实现(更好)..看看“模块模式”(如果你想了解javascript,模块模式是必须阅读的)

http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

于 2012-09-15T23:28:47.543 回答