【问题标题】:Only white pieces allowed to move in chessboard.jschessboard.js 中只允许移动白色棋子
【发布时间】:2015-10-20 08:29:56
【问题描述】:

有没有办法配置chessboard.js,让用户只能移动白棋?

【问题讨论】:

    标签: javascript chessboard.js


    【解决方案1】:

    很确定这个例子很容易适应:

    http://chessboardjs.com/examples#4002
    

    改编代码:

    var onDragStart = function(source, piece, position, orientation) {  
        // if it's not white, don't allow drag
        if (piece.search(/^w/) === -1) ||
            return false;
        }
    };
    
    var cfg = {
      draggable: true,
      position: 'start',
      onDragStart: onDragStart
    };
    var board = ChessBoard('board', cfg);
    

    或者直接使用文档中的示例,但不要更改电路板的方向。

    【讨论】:

      【解决方案2】:

      我不知道这在 3 年后是否仍然相关,但您可以尝试这样的事情:

      if (piece.search(/^w/) === -1) {
           cfg.draggable = false;
      }
      

      我在我的国际象棋拼图网页中使用了类似的东西来在拼图结束时停止所有棋子的移动,它实际上工作得很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多