首页 > 解决方案 > 双击/单击破坏 JS 记忆游戏

问题描述

当用户和我 4 岁的孩子一起测试记忆游戏时,她发现了一个错误。当您双击一张卡片时,它认为它是匹配的。我试图禁用已单击的,this.style.pointerEvents = 'none' 然后将其设置回自动。

这适用于第二个点击的卡片,但不适用于第一个(击败使用它并创建新错误的点!该项目当前部署在:https ://dandavies23.github.io/smoothie-moves/

如果您对我如何更好地做到这一点有任何想法,不胜感激!

      function tumblerLift() {
        let cardId = this.getAttribute('data-id')
        this.style.pointerEvents = 'none';
        cardsChosen.push(fruitVegArray[cardId].name)
        cardsChosenId.push(cardId)
        this.setAttribute('src', fruitVegArray[cardId].img)
        console.log(fruitVegArray[cardId])
        if (cardsChosen.length === 2) {
            setTimeout(checkForMatch, 500)
            this.style.pointerEvents = 'auto';
        }
    } ```

标签: javascriptdouble-click

解决方案


感谢The Bomb Squad 的回答,该null解决方案帮助修复了匹配后出现的另一个水果错误。

这是我现在拥有的代码:

    // Tumbler lift
    function tumblerLift() {
        if (this.src.includes('images/blank.png')) {
            return null
        } // prevents fruit from reappearing, credit Y0urs Truly from Github for helping fix this bug

现在感觉好多了;实际游戏可以在这里玩:https ://dandavies23.github.io/smoothie-moves/


推荐阅读