In a game scenario, where you have an update()
method is called 30 times a second, what would be the best way to do something once?
For example, if you were adding an entity into the scene:
public void update() {
Entity e = new Entity(32, 32, true);
e.add();
}
Because the method is being called 30 times a second, this would be adding 30 entities in one second. The way I usually do it is by creating a boolean:
private boolean entityAdded;
public void update() {
if(!entityAdded) {
Entity e = new Entity(32, 32, true);
e.add();
entityAdded = true;
}
}
Note that this isn't specifically about adding entities, that was just an example. It could be about adding a certain amount of points to the player's score, or something.
But this seems a little messy if you have multiple cases like this, and you're creating temporary variables which can get annoying...
Is there a better way of doing this?