开发者

How do I design "manual animation" into an Android game without code delays

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 mov开发者_C百科e 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();
        }
    }
}


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 to the screen.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜