【发布时间】: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 编号解决了双击匹配问题。