I’m making a game that displays some numbers on a canvas (score, time, etc).
The way that I currently do this is with the drawtext command on a canvas
// score is some int
draw(Canvas c) {
c.drawText(score+"", x, y, paintSyle);
}
I hear that object creation and garbage collection are expensive operations, and I think this is creating a new string every time it is called.
Right now my game with all bitmap drawing and everything jumps around from 25 to 60 fps. I’d like it to stay closer to the higher number and I’m trying to find ways to speed it up.
Would it be faster/better to make(or find?) some mutable subclass of string and work around this problem? Is there another way to solve this issue? Or is this just how it is?
Introduce two new
privatemember variablesString renderedScoreStringandint rederedScoreand rewrite yourdraw()-method like that:that should save you a lot! of object creations. You could also hide the boilerplate code behind a getter method, e.g.
String getScoreString()which does the same, so you don’t have it in thedraw()-method.