0

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();
}
Newman
  • 29
  • 4

1 Answers1

1
  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.

spydon
  • 9,372
  • 6
  • 33
  • 63
  • Sounds great. About second point - that's exactly what i was thinking. It shouldn't be much problem if I'll pass something from parent to child, however, as far as i read - there is a problem with connection in opposite direction. Can you give me some tips or paste some good tutorial? Right now I'm trully missing the point of this whole callback idea. And i read like 4 or 5 articles and all of them weren't really helpful, more like misleading. Or that's just me, idk. Thank you in advance. – Newman Jun 04 '21 at 17:31
  • So you want to create the game in this child and then pass it to the parent, is that what you are saying? – spydon Jun 05 '21 at 13:27
  • 1
    Exactly. I improved my code and with fresh head implemented callback - it works like a charm. Thank you for advice. – Newman Jun 05 '21 at 14:07