Vue匹配游戏一次单击即可移交所有匹配项

问题描述 投票:0回答:1

我正在学习Vue.js,并正在构建一个简单的小马里奥主题纸牌配对游戏进行练习。游戏创建了18张随机洗牌的阵列,玩家应该单击这些卡以翻转它们并尝试进行匹配。问题是单击卡时,它及其所有匹配项也会翻转。但是我想要仅单击已被翻转的卡

这是我的Vue应用程序绑定到的元素:

<div class="container" id="matchGame">
       <div class="col-6 mx-auto bg-dark" style="margin-top:230px;">
           <h3 class="text-light text-center pt-3">Mario Bros Matching Game</h>
           <button v-on:click="start" v-show="!started" class="btn btn-lg btn-success mb-2">Start</button>
           <ul v-if="started" class="d-flex flex-sm-wrap justify-content-between mt-5 pb-5">
               <li class="mb-3 ml-2" v-for="(card, index) in cards" v-on:click="flipCard(card)">
                   <transition name="flip">
                       <span v-on:click="flipCard(index)" v-if="!card.flipped"><img class="card" v-bind:src="cards[index].front" /></span>
                       <span v-on:click="flipCard(index)" v-else><img class="card" v-bind:src="cards[index].back" /></span>
                   </transition>
               </li>
           </ul>
           <button v-on:click="stop" v-show="started" class="btn btn-danger mb-3">Stop</button>
      </div>
</div>

这是我的JavaScript:

// This is all the data for each card
const items = [
    {
        front: 'images/cardfront.png',
        back: 'images/coin10.png',
        name: 'coin10',
        flipped: false
    },
    {
        front: 'images/cardfront.png',
        back: 'images/coin20.png',
        name: 'coin20',
        flipped: false
    },
    {
        front: 'images/cardfront.png',
        back: 'images/flower.png',
        name: 'flower',
        flipped: false
    },
    {
        front: 'images/cardfront.png',
        back: 'images/mushroom.png',
        name: 'mushroom',
        flipped: false
    },
    {
        front: 'images/cardfront.png',
        back: 'images/oneup.png',
        name: 'oneup',
        flipped: false
    },
    {
        front: 'images/cardfront.png',
        back: 'images/star.png',
        name: 'star',
        flipped: false
    }
];


// This is a function to create an array of 18 randomly shuffled cards
// Previously I had a function like it in the methods section of the Vue app but I changed 
// it while trying to solve the issue
function createGameCardsArray() {
    const cards = [];
    for (let i = 0; i <= 17; i++) {
        cards[i] = items[Math.floor(Math.random() * 6)];
    }
    return cards;
}


// Vue app
const app = new Vue({
    el: '#matchGame',
    data: {
        // This holds the array of randomly shuffled cards
        cards: createGameCardsArray(),
        gameSounds: audio,
        started: false
    },
    methods: {
        // When this method is called, all matches are flipped
        flipCard: function(card) {
            card.flipped = !card.flipped;
        },
        start: function() {
            // this.shuffle();
            console.log(this.cards);
            this.started = true;
            this.gameSounds.background.play();
            this.gameSounds.background.loop = true;
        },
        stop: function() {
            this.started = false;
            this.gameSounds.background.pause();
            this.gameSounds.background.currentTime = 0
            this.gameSounds.background.loop = false;
        },
        // shuffle: function() {
        //     const shuffledCards = [];
        //     for (let i = 0; i <= 17; i++) {
        //         shuffledCards.push(gameCards[Math.floor(Math.random() * 6)]);
        //     }
        //     this.cards = shuffledCards;
        // }
    }
});

这是一个令人难以置信的令人沮丧的问题,因为从我的角度来看,应该根据阵列中它们的唯一索引来翻转卡片,但这显然不是正在发生的事情。取而代之的是,所有匹配的卡都被翻转了,因此我无法继续开发具有此问题的游戏。对确定和解决问题根源的任何帮助将不胜感激!

javascript vue.js
1个回答
0
投票

createGameCardsArray创建对items[random index]相同实例的引用的数组,因此从一个引用编辑卡将反映对同一张卡的所有引用的更改。解决方法是使用spread operator(或Object.assign)克隆每个卡:

Object.assign

function createGameCardsArray() {
  const cards = [];
  for (let i = 0; i <= 17; i++) {
    cards[i] = { ...items[Math.floor(Math.random() * 6)] };
  }
  return cards;
}
// This is all the data for each card
const items = [
  {
    front: 'images/cardfront.png',
    back: 'images/coin10.png',
    name: 'coin10',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/coin20.png',
    name: 'coin20',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/flower.png',
    name: 'flower',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/mushroom.png',
    name: 'mushroom',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/oneup.png',
    name: 'oneup',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/star.png',
    name: 'star',
    flipped: false
  }
];


// This is a function to create an array of 18 randomly shuffled cards
function createGameCardsArray() {
  const cards = [];
  for (let i = 0; i <= 17; i++) {
    cards[i] = { ...items[Math.floor(Math.random() * 6)] };
  }
  return cards;
}

new Vue({
  el: '#app',
  data: {
    // This holds the array of randomly shuffled cards
    cards: createGameCardsArray(),
    // gameSounds: audio,
    started: false
  },
  methods: {
    // When this method is called, all matches are flipped
    flipCard: function(card) {
      card.flipped = !card.flipped;
    },
    start: function() {
      console.log(this.cards);
      this.started = true;
      //this.gameSounds.background.play();
      //this.gameSounds.background.loop = true;
    },
    stop: function() {
      this.started = false;
      this.gameSounds.background.pause();
      this.gameSounds.background.currentTime = 0
      this.gameSounds.background.loop = false;
    }
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.