简体   繁体   English

如何为网格创建点击监听器?

[英]How to create click listener for grid?

I'm trying to create grid (for example 5x5).我正在尝试创建网格(例如 5x5)。 After creating grid I want to click on an item and pass coordinates into my onClick(i,j) method.创建网格后,我想单击一个项目并将坐标传递到我的 onClick(i,j) 方法中。 How can I do this using blessed and blessed-contrib?我如何使用 blessed 和 blessed-contrib 来做到这一点?

This is my code.这是我的代码。 This is Gomoku game or Five in a row.这是五子棋游戏或五连胜。

let blessed = require('blessed')
    , contrib = require('blessed-contrib')
    , program = blessed.program()

const Gomoku = require('gomoku-js')

let size = 5

let game = new Gomoku(size)

let order = true // 1=X, 0=O

let screen = blessed.screen()

let grid = new contrib.grid({rows: size, cols: size, screen: screen})

for (let i = 0; i < size; i++) {
    for (let j = 0; j < size; j++) {
        grid.set(i, j, 4, 4, blessed.box, {
            content: '',
            bold: 'bold'
        },)
    }
}

function setChess(i, j) {

    if (order === true) {
        grid.set(i, j, 4, 4, blessed.box, {
            content: 'X',
            bold: 'bold'
        },)
    } else {
        grid.set(i, j, 4, 4, blessed.box, {
            content: 'O',
            bold: 'bold'
        },)
        order = false
    }

    try {
        game.setChessOf(order, i, j)
        order = !order
    } catch (e) {
        console.log(e)
    }

}


screen.key(['escape', 'q', 'C-c'], function (ch, key) {
    return process.exit(0)
});

screen.render()

You need to specify the element to add a callback method, like您需要指定要添加回调方法的元素,例如

    button.on('click', () => { /*callback function from blessed element*/ })

in your case在你的情况下

    grid.on('click', () => { /*callback function from blessed element*/ })

Hope this helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM