chessboardjs icon indicating copy to clipboard operation
chessboardjs copied to clipboard

Figure doubles on change turn to you

Open Zhiz0id opened this issue 11 years ago • 4 comments

There is a bug with snapping figure:

When you get a piece and it is "flying" near the pointer in time of your opponent turn, a new piece will born on place where you got your piece when opponent made the move.

Example: http://chessboardjs.com/examples#5001

Reproduction:

  1. Opponent turn(you can add pause for random opponent turn)
  2. Pick up piece(but do not drop it)
  3. Opponent made the move
  4. You see second piece.

image

Zhiz0id avatar Jul 17 '14 12:07 Zhiz0id

I believe this happens because drawPositionInstant() is called when the move animation finishes. This causes the dragged piece to be readded to the board. Adding the following to drawPositionInstant() after the loop fixes a similar problem I had, though I haven't tested with example 5001.

if (DRAGGING_A_PIECE) {
  // make sure dragged pieces don't suddenly reappear
  $('#' + SQUARE_ELS_IDS[DRAGGED_PIECE_SOURCE]).addClass(CSS.highlight1)
    .find('.' + CSS.piece).css('display', 'none');
}

(There is a similar problem if you drop a piece while an animation is running. The call to drawPositionInstant() will cause a duplicate of the animating piece to appear on the target square. I don't know how to fix this; it might involve keeping track of all destination squares for animations and then not updating those squares in the drawPositionInstantFunction())

sorensp avatar Dec 07 '14 03:12 sorensp

It's been a while since I looked at this code, but I believe @sorensp is correct here.

I think I used drawPositionInstant() to just "make sure" that the board was in a valid state after animations finished, but tracking the state of the animations is probably what needs to happen instead.

oakmac avatar Dec 07 '14 03:12 oakmac

Thanks but this solution runs into all sorts of disappearing piece problems in cases where the attempted move is illegal. Take a look at this solution: https://github.com/oakmac/chessboardjs/pull/86

It still has problems, but gets a bit closer.

user14159 avatar Mar 18 '16 19:03 user14159

What's the status on this? Maybe another solution is to allow the user to supply a callback to be invoked when the animation finishes. This way, you can restrict picking up a piece (drag start) until the "opponent's" turn finishes.

jeremyong avatar May 16 '19 11:05 jeremyong