Skip to content

Conversation

@hughescr
Copy link

@hughescr hughescr commented Jan 6, 2026

Summary

Fix add() method firing completion handlers before dice animation completes.

Problem

When calling add() to add dice to an existing roll, the completion handlers (onAddDiceComplete callback, addDiceComplete event, and Promise resolution) fire immediately on the first animation frame instead of waiting for the dice to settle.

Root Cause

In the add() method:

  1. simulateThrow() pre-simulates the physics to completion, leaving newly added dice in SLEEPING state
  2. Dice are re-spawned for visual animation
  3. animateThrow() starts with the completion callback
  4. On the first frame, throwFinished() checks if all dice have sleepState >= SLEEPING — they do (from pre-simulation) — so the callback fires immediately

Why roll() works correctly

The roll() method calls clearDice() first, which removes all existing dice. Fresh dice start in an active state and need time to settle, so throwFinished() correctly waits.

The Fix

After spawning dice and before starting animation, wake up the newly added dice so they need time to settle during the visible animation:

  for (const idx of diceIdArray) {
      const die = this.diceList[idx];
      if (die && die.body) {
          die.body.wakeUp();
      }
  }

This ensures throwFinished() won't return true until the new dice actually settle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant