【问题标题】:Double tap/click ruining JS memory game双击/单击破坏 JS 记忆游戏
【发布时间】:2021-08-31 12:51:00
【问题描述】:

在与我 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';
        }
    } ```

【问题讨论】:

  • 一种选择是通过this.getAttribute('src')检查src,看看它在做任何事情之前是否还没有被翻转。
  • 我这样做是合法的 ;-; @乔
  • src 与重复数组以创建对的相同。 fruitVegArray = [...fruitVegArray, ...fruitVegArray] 实际上,我通过比较用于形成网格的 data-id 编号解决了双击匹配问题。

标签: javascript double-click


【解决方案1】:

好吧,我想你可以尝试使用像这样的另一个 if 语句(基于你的 setAttribute

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

【讨论】:

    【解决方案2】:

    感谢an answer by The Bomb Squadnull 解决方案帮助修复了水果匹配后出现的另一个错误。

    这是我现在拥有的代码:

        // 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/

    【讨论】:

    • 感谢您回来分享您的成果/成功; solve your own problem 总是好的!请注意,我们需要 Stack Overflow 上的所有个答案,以便为问题提供完整且独立的解决方案。我没有足够的专业知识来了解这是否真的提供了完整的解决方案。如果没有,可以请edit 扩展它吗?请确保您已阅读我们的How to Answer
    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2017-03-15
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多