views:

50

answers:

3

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

The architecture is basically two threads, one that just continuously draws the screen and the other than controls the movement of the drawables on the screen based on the user's touch screen input.

In the latter thread, I'm basically doing manual animation (insert correct term here), meaning I move the drawables on the screen via methods that change the Drawables Rect as the game progresses. The "no-no" I believe I'm doing is inserting delays into these methods, so that it "appears like an actual animation."

This all works fine and dandy because I have no reason to process any user input during these animations, but it's really bugging me that I'm designing a flaw into a lot of hard work with my game. I'm sure many of you can relate to this in terms of code design.

What is the proper way to design this "manual animation" so my code can still process user events (like touching the screen), while the animation is occurring?

Here is a reference example of 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();
        }
    }
}
A: 

Still hoping to find an answer...perhaps my question is vague.

I think it is probably due to your low accept rate.
Biff MaGriff
A: 

The best way to do this is inside your game loop. You have a timer that you have running if it has elapsed perform your draw, reset the timer. Otherwise do nothing.

Biff MaGriff
+1  A: 

This is an excellent 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 covers how to accept user input whilst simultaneously changing the game state and drawing the to screen.

teedyay
Wow, that's a mouthful. I will read through that this weekend, thanks!