How to create "manual animation" in an Android game without code delays

I am writing a game for Android, and I think I have a fatal error in my architecture and / or code.

The architecture consists mainly of two streams: one that simply draws the screen continuously, and the other controls the movement of drawings on the screen based on user input on the touch screen.

In the last thread, I mainly do manual animation (insert the correct term here), that is, I move the drawings on the screen using methods that change the Drawables Rect as the game progresses. "No, no," I suppose I'm doing this nesting of delays in these methods, so that it "looks like a real animation."

All this works fine and dandy, because I have no reason to handle user input during these animations, but it really pushes me to develop a flaw in the big hard work with my game. I'm sure many of you can relate to this in terms of code design.

What is the correct way to develop this “manual animation”, so my code can still handle user events (like touching the screen) when the animation happens?

Here is an example link to one of my methods:

public void BadAnimation() { for (int k = 0; k < mAnimationHeight; k++) { for (int i = 0; i < mRows; i++) { for (int j = 0; j < mCols; j++) { if (myObject.mFlag[i][j]) { myObject[i][j].mRect.top++ } } } try { Thread.sleep(3); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 
+4
source share
2 answers

This is a great tutorial that will show you how to implement the delay correctly:

http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html

It also describes how to receive user data while changing the state of the game and drawing on the screen.

+3
source

The best way to do this is inside the game loop. You have a timer that you use, if it has expired by completing your draw, reset the timer. Otherwise, do nothing.

0
source

All Articles