flutterdartflameforge2d

Flame game stops running when I change the screen


I have created a simulation using Flutter+Flame+Forge2d. When I change to another screen, the simulation stops and only continues when I move back. This happens when I swipe to right with my MacBook Air so that I can use another program while the simulation is running. This happens in Chrome web but not in macOS desktop.

Is there away to keep the program running even when it's not currently showing in the display?

//Here is a code to reproduce the issue

import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:flame/components.dart';

void main() {
  runApp(GameWidget(game: Game()));
}

class Game extends Forge2DGame {
  TextPaint textPaint = TextPaint(
    style: const TextStyle(color: Colors.red),
  );
  int _counter = 0;

  Game();

  @override
  void update(double dt) {
    super.update(dt);
    _counter += 1;
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
    textPaint.render(canvas, "Flame is awesome $_counter", Vector2(10, 10));
  }
}
dependencies:
  flame_forge2d: ^0.12.4
  flame: 1.5.0
  flutter:
    sdk: flutter

Solution

  • For web this is not currently possibly unfortunately since the game loop stops running when it is not rendering anything, which it won't when the browser tab isn't focused.

    Just like you discovered it should work for desktop builds though.