Book Image

Learning Dart

Book Image

Learning Dart

Overview of this book

Table of Contents (19 chapters)
Learning Dart
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Animating a game


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...