react-native-game-engine: Can't destroy previous entities when game reloads

I’ve got system function to handle events like this way:

export default (entities, { events }) => {
  events.forEach((event) => {
    switch (event.type) {
      case EV_CONTROL_CAMERA_SCALE:
        world_scale(entities, event.value, event.duration);
        break;
      default:
        break;
    }
  });

  return entities;
};

And function world_scale looks like:

const world_scale = (entities, scale_value, duration) => {
  const interval_timing = 30;
  const tick = Math.floor(duration / interval_timing);
  const ratio = (scale_value - 1) / tick;
  let counter = 0;
  const interval = setInterval(() => {
    Object.keys(entities).map((objectKey) => {
      entities[objectKey].body.scale += ratio;
    });
    counter++;
    if (counter === tick) {
      clearInterval(interval);
    }
  }, interval_timing);
};

This function scales all the entities smoothly in given duration, constantly. Like camera zooming. Function works perfectly until i exit the game by navigating into the other component and re-enter to the game component. (I’m using react-native-router-flux to navigate) The problem is when i re-navigate into the game and dispatch the event EV_CONTROL_CAMERA_SCALE, then function world_scale will be working with previous entities (which should be not existing). But in the meantime entities already rendered on the screen and not scaling.
I think navigation is not the problem source. Every time when i navigate, i clear the previous state by using Actions.reset

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve found the bug… The GameEngine uses a timer to run the systems at ~60 fps. However, there is a bug with how I’m clearing subscribers during unmounting which means a reference to the update function (and potentially the entire GameEngine) was being kept in memory - hence multiple GameEngines could be running at the same time.

This bug has been fixed in version 0.9.5