Skip to content

[GREENFOOT-151] Method call dialog executes methods on the event thread

When invoking a method on an actor in the Greenfoot world, the invoked method will run on the event thread. This is a problem if the method is invoking code that involves updating the ui like repaint() or delay(int). An example that doesn't work when added to the Wombat class \- the world is only repainted after all steps has finished:

{noformat} /** get the number of steps from the Method-Call and move

  • that number opf steps ahead */ public void move(int steps) { if (steps < 1 || steps > getWorld().getWidth()) return; for (int i=0; i<steps; i++) { move(); } } {noformat}

A workaround is to execute the repaint/delay code on another thread (for the wombat class):

{noformat} public void move(final int steps) { if (steps < 1 || steps > getWorld().getWidth()) return; new Thread() { public void run() { for (int i=0; i<steps; i++) { move(); Greenfoot.delay(1); } } }.start(); } {noformat}


Issue metadata

  • Issue type: Bug
  • Priority: Medium
  • Fix versions: 1.4.6