The Problem
I’m developing an AR board game. The game is designed to work with multiple image targets, but I’m facing issues with managing the game state across these targets. Here’s what I’m trying to achieve:
- The game should be playable on any of the image targets.
- Once a game starts on one target, it should become the “active” target.
- If the player then points the camera at a different target, the game elements should appear, but the dice should be disabled (as the game is in progress on another target).
- The player should be able to return to the active target and continue their game.
Current Behavior
Currently, the game is behaving inconsistently:
- Sometimes it resets when switching between targets.
- The dice enables/disables inconsistently when detecting new targets.
- The game state (started/not started) isn’t maintaining correctly across target changes.
Relevant Code
Here’s a simplified version of my image detection handling:
el.sceneEl.addEventListener('xrimagefound', (event) => {
const targetName = event.detail.name;
if (validTargets.includes(targetName)) {
if (gameStarted && activeTarget !== targetName) {
console.log("Game in progress. Dice is disabled on this target.");
disableDice();
} else if (gameStarted && activeTarget === targetName) {
console.log(`Returning to active game on target ${targetName}.`);
enableDice();
} else {
console.log(`New game starting on target ${targetName}.`);
gameStarted = false;
activeTarget = null;
enableDice();
}
}
});
The Question
How can I effectively mark one target as the “active” target and maintain this state even when the camera moves to other targets? Is there a best practice for managing game state across multiple AR image targets?
Any insights, code snippets, or suggestions would be greatly appreciated. Thanks in advance for your help!