Help Needed: AR Game with Multiple Image Targets - How to Mark One as Active?

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:

  1. The game should be playable on any of the image targets.
  2. Once a game starts on one target, it should become the “active” target.
  3. 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).
  4. 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!


Have you tried storing the current name of the active image target in a global variable, then checking if the name of the newly detected image target matches it before taking any action?

did it. it doesn’t work. i even created a flag for the game. but nothing works.
in console i found that even a little bit of shake was making the image to retarget it. and multiple image found logs. that is why the game was getting confused and it starts from the beginning and not working properly

It sounds like your image targets might be losing tracking due to a lack of detail.

Have you had a chance to review our guide on optimizing image target tracking? You can find it here: