I just started android studio. I am currently making a game. I read that you should separate ui and game logic / rendering on different threads. I currently have a class game that extends activity running on the ui-thread. Game also has a handler and a Game View class. The Game View is the game world, it is run on a separate thread i.e worker thread. I am trying the wrap my head around how to communicate between these, since for example i need to update the health bar on the ui thread when the player takes damage on the worker thread.
Fortunately i have a reference to the handler in my Game View class, so i should be able to communicate between these. My issue is that publish takes a log record as a parameter which would need to be deciphered in the other end using a switch. Would it instead be a good practice the create a custom handler that has appropriate methods like this:
this.handler = new CustomHandler() {
public void updateHealthBar(int damage) {
//find ui element and update ...
}
...Other methods
@Override
public void publish(LogRecord logRecord) {
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
};
Instead of this:
this.handler = new CustomHandler() {
@Override
public void publish(LogRecord logRecord) {
switch(logRecord.Message()) {
case "Update Health Bar":
//find ui element and update ...
... check for other cases
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
};
How do i even get the integer value that i should update the ui with from a logRecord? I need one variable that says what to update but also some value that says by how much...