0

I'm doing a simple match-3 game and I'm currently using a game loop like the code below. I'm using some frame calculations for optimal speed of th game. I took this from another game that I have done before. But this seems unnecessary for this type of game.

I need to have some time delays at several places, like when I have moved an object on the game area, I want a short time delay before the call to an method that check if there is a match of three objects in a row. And then I also need a short time delay when the code has detect a match, so that I can do some simple effect at those positions in the game grid before the user starts to move another object and then check for a match.

As it is now every thing happens at once and I wonder if it would be better to run this game without this calculations of frames and how I could do instead to get some time delays? I have tested to use Thread.sleep(250) inside this code, but didn't worked the way I hade hoped for.

What could be a better approach for running a game like this?

// Game loop ---------------------------------------
@Override
public void run() {
    // TODO Auto-generated method stub

    long beginTime;
    long timeDiff;
    int sleepTime;
    int framesSkipped;

    sleepTime = 0;

    while (gameRunning) {
        if (!surfaceHolder.getSurface().isValid())
            continue;

        try {
            canvas = surfaceHolder.lockCanvas();

            beginTime = System.currentTimeMillis();
            framesSkipped = 0;

            // Different game states
            switch (gameState) {

            case 0: // Intro game
                drawStartPage(canvas);
                break;

            case 1: // Play game
                canvas.drawRGB(0,0,0);

                if(touchActionDown) {
                    touchActionDown = false;
                    colorObjectManager.checkPosition(touchX, touchY);
                    touchActionMove = false;
                }

                if(touchActionMove) {
                    touchActionMove = false;
                    colorObjectManager.swapObject(moveDirection);
                    // Time delay
                    colorObjectManager.checkMatch();
                    // Time delay
                }

                // Call method to draw objects on screen
                colorObjectManager.drawObjectsList(canvas);
                break;

            case 2: // End game

                break;

            }

            // Calculate difference from first call to
            // System.currentTimeMillis() and now
            timeDiff = System.currentTimeMillis() - beginTime;
            // Calculate sleepTime
            sleepTime = (int) (FRAME_PERIOD - timeDiff);

            if (sleepTime > 0) {

                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                }
            }

            while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                // Call method to only update objects on screen
                updateObjects();
                sleepTime += FRAME_PERIOD;
                framesSkipped++;
            }

        } finally {

            surfaceHolder.unlockCanvasAndPost(canvas);

        }

    } // End while-loop
}

// End game loop ---------------------------------------
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159

2 Answers2

0

I suggest use libgdx its a simple frameWork and you dont need to Calculate the DeltaTime. AFAIK Dont use the Thread sleep on a surfaceholder use a seperate thread and lock the canvas.

u.jegan
  • 833
  • 6
  • 15
  • I know about libgdx and some other frameworks, but I have already done so much coding now. Could you explain a little bit further what you mean with what you are writing in the last sentence? Code example? – 3D-kreativ May 29 '13 at 07:12
  • @3D-kreativ : `Thread.sleep` is not suitable for use , as sometimes it becomes an essential part of a poorly designed program . Look here : http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx. If you want to apply `sleep` state, then you can probably try out `TimerTask`: http://stackoverflow.com/questions/1453295/timer-timertask-versus-thread-sleep-in-java – The Dark Knight May 29 '13 at 07:16
  • @TheDarkKnight OK, not sure how I should continue, but I guess there would be better to use a TimerTask then. I have to test some more. – 3D-kreativ May 29 '13 at 07:25
0

you can use android animations. fadein fadeout to be fancy.

//some other method to delay
new Handler().postDelayed(new Runnable()
{
 @Override
 public void run()
 {
   // your code here
 }
}, 1000/* 1sec delay */);

also got a very lame solution

 //for 5 second of delay
 for(int i=0;i<5000:i++){
    for(int k=0;k<5000:k++){
       for(int j=0;j<5000:j++){

       }
    }
  }
Alp
  • 1,863
  • 1
  • 20
  • 38