Search code examples
flutterflame

Two questions about Flame Engine


I have some questions. I'm working over app which uses Flame Engine in Expanded Widget as a part of bigger application. So Flame is used only as graphic representation of buttons and for interaction.

Now, theoretically:

  1. Is it possible to send variable value from flame to regular Text widget in my app? (I know I can read initial value which is 0, but then nothing happends even if console shows incrementing value) Should I write some kind of stream for Flame and future in main app, or is there any other option?
  2. How can I manage views in flame? I mean that currently I have two different backgrounds and different animations for each. On the bottom of the screen in main app in flutter i have buttons which i would like to use to change flame background and animations as needed (simple response from flame to my buttons)

As for code, my counter in app looks like this:

...
Column(
   children: [
     Text('show 1'),
     Text(_firstCounter.toString())
   ],
),
...
void CountersAndOthers() async {
  if(_gameInProgress == true){ return; }
  _gameInProgress = true;

  _testGame = new TestGame();
  _firstCounter = _testGame.counterNumeroUno;
}

And in Flame it's a simple onTap() function that increment int counter:

@override
void onTap() async {
  counter++;
  print(counter);
  jump();
  _timer.start();
}

Solution

    1. Without using any state management library you can pass in a callback function for you widget to your extended Flame game class and update the state for the widget through that when Flame's onTap is called.

    2. Here it is the other way around, pass your game class to the navigation buttons and call a function that you make in your flame game that will react to your button presses.

    There are multiple more ways of doing this depending on the structure of your app.