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

时间:2012-09-15 23:20:51

标签: javascript html

温柔,我对JavaScript非常陌生:

我正在创建一个二十一点游戏 - 关于CodeAcademy的一些教程涵盖了一些基础知识,所以我试图自己进一步采取一些步骤。作为打印每个玩家姓名的先驱,我创建了一个可以打印“Bang,Boom!”的功能(showNames())。进入我的“hud”部分。当我(暂时)将showNames()分配给我的“点击”按钮的onclick功能时,这种方法有效。但是,当我尝试从showNames()函数调用setupGame()时,它不会触发。我哪里错了?代码如下:

的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 + " ";
    }
}

2 个答案:

答案 0 :(得分:3)

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

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

DEMO

答案 1 :(得分:1)

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

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

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