简体   繁体   English

在JQuery函数之间调用变量

[英]Calling variables between JQuery functions

I'm new to JS, trying to build a game based around switching a set of tiles back and forth between two players. 我是JS的新手,他试图构建一种基于在两个玩家之间来回切换一组拼贴的游戏。

One is function intended to switch turns between two players. 一种功能是用于在两个玩家之间切换回合。 Another changes the tiles' color, using the turn variable to determine which color to switch to. 另一个更改砖的颜色,使用turn变量确定要切换到的颜色。

I don't think either is working. 我认为两者都不起作用。

Fiddle: http://jsfiddle.net/f5mEY/3 小提琴: http : //jsfiddle.net/f5mEY/3

var currentPlayer;
var playerOne;
var playerTwo;
var turnCounter = 0;

//this function is intended to iterate between the two players when a player completes their turn by hitting submit
$(document).ready(function() {
    $('#submit').click(function() {
        turnCounter += 1;
        if (turnCounter%2 === 0) {
            currentPlayer = playerOne;
        }
        else {
            currentPlayer = playerTwo;
        }
    });
});

//and this should turn the unplayed tiles to the correct player's color when they are clicked
$(document).ready(function() {
    $('.unplayedTile').click(function() {
        if (currentPlayer === playerOne) {
            $(this).toggleClass('unplayedTile playerOneTile');
        }
        else {
            $(this).toggleClass('unplayedTile playerTwoTile');
        }
    });
});

Many thanks in advance for any help! 在此先感谢您的帮助!

You need to initiate playerOne and playerTwo with something that can be compared. 您需要使用可以比较的方式来启动playerOneplayerTwo Currently, playerOne and playerTwo are undefined which makes currentPlayer === playerOne always true 当前, playerOneplayerTwoundefined ,这使得currentPlayer === playerOne始终为true

In your jsFiddle, the variables playerOne and playerTwo never get anything assigned to them so therefore they are both undefined . 在您的jsFiddle中,变量playerOneplayerTwo从未分配任何东西,因此它们都是undefined If they have the same value, then you can't tell the difference between them in your if statements. 如果它们具有相同的值,则无法在if语句中分辨它们之间的区别。

I don't know exactly how you intended to use those variables, but if you just assign them some initial value that is different between them, then your comparisons for who is the current player should start working. 我不知道您打算如何使用这些变量,但是如果您只是给它们分配一些初始值,而这些初始值在它们之间是不同的,那么您应该比较谁是当前玩家。

var playerOne = 1;
var playerTwo = 2;
var currentPlayer = playerOne;

PS If you're trying to actually use the jsFiddle, you have to select the jQuery lib in the left hand panel so jQuery is available. PS:如果您尝试实际使用jsFiddle,则必须在左侧面板中选择jQuery lib,以便使用jQuery。

The problem is you are not assigning any values to the player constants. 问题是您没有为播放器常量分配任何值。

var currentPlayer = 1;
var playerOne = 1;
var playerTwo = 2;
var turnCounter = 0;

Demo: Fiddle 演示: 小提琴

Here the initial values are set so that currentPlayer will be the playerOne 在这里设置初始值,以便currentPlayer将成为playerOne

Note: Also in the fiddle jQuery was not included 注意:同样在小提琴中不包括jQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM