People like motion in games and a movie is nothing but a quick succession of image frames. So, we need to be able to redraw our screen periodically to get that effect; with Dart screen frame rates of 60 fps or higher, this becomes possible. A certain time interval is represented in Dart as an object of the type Duration
. To do something periodically in Dart, we use the Timer
class from the dart:async
library and its periodic
method. To execute a function moveBall()
at every INTERVAL
ms
(you could call it a periodic event), use the following method:
new Timer.periodic( const Duration(milliseconds: INTERVAL),(t) => moveBall() );
The first parameter is the time period, the second is the callback function that has to be periodically executed, and t
is the Timer
object. If the callback function has to be executed only once, just write a new Timer(.,.)
method, omitting the periodic
function. When drawing on canvas, the first thing that the periodically called function will have...