在记忆游戏中翻转牌

时间:2016-07-15 08:08:04

标签: javascript android html

我正在建立一个记忆游戏,当你选择两个时,我无法弄清楚如何翻转我的牌?目前我的卡片是静态的,当你点击两张卡片时,我有一个警告框,上面写着“你有一个匹配”或“抱歉没有匹配”。

  1. 如何翻转卡片?

  2. 我在哪里放回卡片的图像?我试过把它放进去 我的主要Javascript文件,但它不起作用?

  3. 有什么想法吗?

                //var cardOne = "queen";
                //var cardTwo = "king";
                //var cardThree = "queen";
                //var cardFour = "king";
    
                //console.log("hello");
    
                //f (cardTwo === cardFour) {alert ("You found a match!")}
                    //else {alert ("Sorry, try again")}
            var gameBoard = document.getElementById("game-board");
            var cards = ["queen", "queen", "king", "king"];
            var cardInPlay = [];
    
                var createBoard = function () {
     for (var i = 0; i < cards.length; i++) {
       var newCard = document.createElement('div');
       newCard.className = "card";
       newCard.setAttribute('data-card', cards[i]);
       newCard.addEventListener('click', isTwoCards);
       gameBoard.appendChild(newCard);
    
         }
    };
    
    
    
            function isTwoCards() {
                  cardInPlay.push(this.getAttribute('data-card'));
                    if (cardInPlay.length === 2) {
                         isMatch(cardInPlay);
                         cardInPlay = [];
    
            }
            };
            var isMatch = function(array){
     (array[0] === array[1]) ?   alert("You found a match!") :  alert("Sorry,    try again.");
       };
    
    
     createBoard();
    

1 个答案:

答案 0 :(得分:1)

试试这个:

cover   cover
card    card

将封面的宽度设置为0,同时将卡片的宽度设置为其宽度。

或者使用jquery,更容易: https://nnattawat.github.io/flip/