制作可点击的卡片对象

时间:2017-09-11 17:19:02

标签: javascript class playing-cards

我正在尝试复制UNO - 纸牌游戏。所以我想要生成卡片对象,如果它适合堆叠(如果它是相同的颜色或值)。我设法让每个对象渲染。但我无法让它如何让它互动。欢迎任何建议。

class Card {
constructor(v, n, c) {
    this.v = v;
    this.n = n;
    this.c = c;
}
onClick() {
    if (this.v === pile[pile.length-1].v || this.c === pile[pile.length-1].c) {
        pile.push(this);
        deck.splice(this, 1);
    }
}
render() {
    let cl = `card num-${this.v} ${this.c}`,
        div = document.createElement('div')
    if (this.c) {
        cl = `card num-${this.v} ${this.c}`
    } else {
        cl = `card num-${this.v}`
    }
    div.innerHTML = this.v > 10 ? this.n : this.v
    document.body.appendChild(div)
    div.classList.remove
    div.className = cl
    document.getElementsByClassName(cl).addEventListener('click', this.onClick())
    }
}

0 个答案:

没有答案