0

So i am making a game and have three things that could potentially be required to run concurrently.

showDamage - displays a number (in a TextView) for a short moment (~300 milliseconds) then disappears.

delay - for holding a temporary boost. A TextView is also updated at the beginning and end. This is long (~10 seconds).

cooldown - provides textual feedback for the user on time remaining until a button can be pressed again.

I did some browsing and, after trying a Handler which blocked other events, came to the general opinion that i should be using ASyncs as an easy way to make concurrent threads that make changes to the UI and do tasks without blocking the main thread. So i implemented all 3 as Asyncs. I then came upon a problem where they would interfere with each other - the numbers from the showDamage would hang around for longer than 300ms, the temporary effects would become permanent (the postExecution was never run). Further browsing suggested that Android by default has a pool size of 5 and thus i could only have 5 Asyncs running a time. Using MyASync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ...) helps a bit but it provides parallelism not concurrency.

I've used a CountdownTimer for cooldown and that seems to have stopped the issue with the temporary effects becoming permanent but the TextViews from showDamage still hang if enough is spammed. Is there a fix for this, and are there any attempts i've probably implemented incorrectly?

1 Answers1

0

You shouldn't have individual asynchronous tasks for each component you are trying to update. In the context of your game it sounds like you are having difficulty implementing the architecture for a game loop/engine.

You could use one thread, which would probably be a proper Thread rather than an AsyncTask, due to it's longevity. The idea is that you have a single thread run for the duration of the game which loops over processes data and updates the UI.

The examples in these resources might be overkill, as they are directly drawing instead of using android UI, but they may be helpful for understanding the structure. In your case you would want to make sure you hold references to your TextView and other elements (instead of findById everyt time) for efficiency.

Game loops, explained in incremental complexity

Long read on game loops from Game Programming Patterns textbook

Android game loop using canvas

I find this stackoverflow answer as well as this stackoverflow answer useful for understanding the different concurrency options in Android, though I think you will find the game engine resources more useful.

Community
  • 1
  • 1
bradkratky
  • 1,577
  • 1
  • 14
  • 28