Book Image

Learning Dart, Second Edition - Second Edition

By : Ivo Balbaert
Book Image

Learning Dart, Second Edition - Second Edition

By: Ivo Balbaert

Overview of this book

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

Animating a game


We 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 this 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 Duration type. To do something periodically in Dart, we use the Timer class from the dart:async library and its periodic method. To execute a moveBall()function 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. While drawing on the canvas, the first thing that the periodically called function...