1

I want to display a shot for a game I'm working on. But the canvas just invalidate at the end of my for-loop. It's the same problem like here Canvas do not update (invalidate) until whole while loop ends But the answer doesn't work for me. How can I invalidate my canvas in this case?:

else if (event.getX() > 1150 && event.getX() < 1300 
          && event.getY() > 550 && event.getY() < 700)
  {
    System.out.println("Schuss");
    final ArrayList<Shot> positions = game.shot(100, 45, canvas);
    new Thread(new Runnable()
    {
        public void run()
        {
            for (int i = 0; i < positions.size(); i++)
            {
                game.setCurrentShot(positions.get(i));
                System.out.println(i);
                canvas.postInvalidate();
            }       
        }
    }).start();
}

In the shot-function I calculate all positions. The shot will pass and then I want to display thoos positions on my canvas, but that dosen't work.

Reporter
  • 3,897
  • 5
  • 33
  • 47
DoFlamingo
  • 232
  • 1
  • 18

1 Answers1

0

Try this:

else if (event.getX() > 1150 && event.getX() < 1300 && event.getY() > 550 && event.getY() < 700)
{
    System.out.println("Schuss");
    final ArrayList<Shot> positions = game.shot(100, 45, canvas);
    YourActivity.this.runOnUiThread(new Runnable() {
        public void run() {
            for (int i = 0; i < positions.size(); i++)
            {
                game.setCurrentShot(positions.get(i));
                Log.d("YourApp", "i = "+i);
                canvas.invalidate();
            }
        }
    });
}
10101101
  • 193
  • 1
  • 13