开发者

How to do an update loop?

Im doing a little app, its a memory game, you choose one card, it turns up, you choose the second card, it turns up, if they are the same they are out of the game, if they dont match, they are turned down again.

I have public class PlayActivity extends Activity implements OnClickListener. The flip events are trigged by click handlers, declared at public void onCreate(Bundle savedInstanceState) they work fine.

When the first card is selected, it calls my method Action, this sets the image from default (the card back) to the 'real' image (the card front). Fine so far. My problem is the second card: when its selected, it calls method Action, where it should set the front image (lets call it 'middle action'), then a litle pause (a while loop doing nothing until x milliseconds), and then it checks what to do (if they match or not) and turn them down or take the out of the game. You can see where is the problem: the screen only displays the result after x milliseconds (the 'middle action' is not being draw).

Since I have done some little games with XNA, I know the loop Update-Draw, so I know here im updating the same thing twice so always the last one is drawn. But here, the only updating I can have is when click events are trigged, I need a periodic, con开发者_如何学Pythonstant update.

Help?


You can probably use a TimerTask in order to handle that. You can implement it like the following.

This probably isn't the most robust way to do it, but it is an idea. If I figure out a better way to do it in a short time I'll edit my post. :)

Also I would like to add that if you want to make a game that uses an update / draw loop you may need to use a SurfaceView to draw your game. Look at the example here http://developer.android.com/resources/samples/JetBoy/index.html

public class TestGameActivity extends Activity {

    /* UIHandler prevents exceptions from
     performing UI logic on a non-UI thread */
    private static final int MESSAGE_HIDE_CARD = 0;

    private class UIHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_HIDE_CARD:
                ImageView cardView = (ImageView) msg.obj;
                cardView.setImageResource(R.drawable.faceDownCard);
                break;
            }
        }
    }
    private UIHandler handler = new UIHandler();

    // Handle my click. V is the card view
    public void onClick(View v) {

        final int viewID = v.getId();
        // Create a hide task
        TimerTask hideTask = new TimerTask() {

            @Override
            public void run() {
                // Construct a message so you won't get an exception
                Message msg = new Message();
                msg.what = MESSAGE_HIDE_CARD;
                msg.obj = findViewById(viewID);
                handler.sendMessage(msg);
            }
        };
        // Schedule the task for 2 seconds
        new Timer().schedule(hideTask, 2000);
    }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜