Glen
Glen

Reputation:

Why isn't view.invalidate immediately redrawing the screen in my android game

I am trying to make an android game.

I have a game class that extends activity and handles all the user input. Then I have a missionView class that extends view and it draws the level on the screen.

When the user clicks on a door I want to add some animation.

What happens is: The game calls door.open. Changes the state so the view.onDraw function will draw the door half open. The game calls view.invalidate, which should redraw the screen. Then the game sleeps for half a second. Then it calls door.open again. The second time the function is called it changes the state so the view.onDraw function draws the door completely open. Then the game calls view.invalidate again.

The problem is that it does not redraw the screen when it gets to view.invalidate. If I set a break point on the line and run the debugger and click step into it does not step into my view.onDraw function. It can't even show me the code it is executing.

What I have is: Door class:

public boolean open()
{
   if (doorState == DoorState.Closed)
   {
       doorState = DoorState.Opening;

       return true;
   }
   else if (doorState == DoorState.Opening)
   {
        doorState = doorState.Open;
        return true;
   }
   else
   {
      return false;
   }
}

Game class:

if (tile instanceof DoorTile)
{
    DoorTile doorTile = (DoorTile) tile;
    Door door = doorTile.getDoor();

    if (door.isClosed())
    {
        door.open();
        selectedEntity.openDoor();
        view.invalidate();    // This line does not work

        try 
        {
            Thread.sleep(500);
        } 
        catch (InterruptedException e1) 
        {
             // TODO Auto-generated catch block
             e1.printStackTrace();
        }
        door.open();

        // Handled touch event so break switch statement
        break;
     }
}

Upvotes: 15

Views: 41062

Answers (4)

Hiren Patel
Hiren Patel

Reputation: 52790

I could be late but I'm sure this would work for you.

I did this with Kotlin. You can modify according to Java.

Put your code inside runOnUIThread().

Sample code:

activity?.runOnUiThread { 
  // Your code goes here
}

Hope this helps.

Upvotes: 0

Vadim Guzev
Vadim Guzev

Reputation: 320

I've faced with the same problem in my application.

In my case I was trying to call invalidate() from the [doInBackground()][1] method of the [AsyncTask][2].

The problem was solved by moving invalidate() to [onProgressUpdate()][3] method of AsyncTask. I.e. you cannot update UI from the doInBackground() method of AsyncTask. It is mentioned in Android docs, but it is so easy to forget about it... so, don't forget to check it.

Upvotes: 0

JaanusSiim
JaanusSiim

Reputation: 2192

For games I would actually use a lower level access to graphics. The LunarLander from Google is an excellent example for that. Basically You need to do two things:

  • instead of View use SurfaceView
  • get handler from the SurfaceView
  • replace view.invalidate() with custom method repaint() that would contain

    private void repaint() {
      Canvas c = null;
      try {
        c = surfaceHolder.lockCanvas();
        paint(c);
      } finally {
        if (c != null) {
          surfaceHolder.unlockCanvasAndPost(c);
        }
      }
    }
    

Paint method will contain what You have in View.onDraw at the moment

Upvotes: 15

rndmcnlly
rndmcnlly

Reputation: 1618

View#invalidate tells the system to redraw (via onDraw) the view as soon as the main thread goes idle. That is, calling invalidate schedules your view to be redrawn after all other immediate work has finished. If the code in your Game class is being called from the main thread and it puts that thread to sleep, you aren't just pausing rendering, you are pausing input processing all together (usually a bad idea). As a rule of thumb, never sleep a thread that you didn't spawn yourself unless you know what you are doing.

If you'd like to have your game logic periodically delay using Thread#sleep then run it in a separate thread and use view.postInvalidate() to signal the main thread to wake up and call onDraw.

Upvotes: 24

Related Questions