Animation using a timer

39 views
Skip to first unread message

Bruce Sherwood

unread,
Apr 13, 2013, 1:52:37 AM4/13/13
to glowscri...@googlegroups.com
An animation loop needs to let the window be updated periodically, as otherwise you would never see a display. A convenient way to do this is to place a "rate" statement inside the loop. For example, "rate(200,wait)" means "execute this loop about 200 times per second, waiting periodically, about 60 times per second, for the window to be updated".

There is another way to manage an animation, a way that is more standard in JavaScript programs and can be useful in certain kinds of situations. Instead of putting your calculations inside a loop, you put them inside a function and arrange to have that function executed periodically. As a practical matter, the highest rate of executions you can get is about 60 per second, which is tied to the rate of window updates. If you use this timer-oriented scheme, but you need to perform about 200 calculational updates per second, you need to do about 3 calculational updates every time your function is executed (60*3 = 180 calculational updates per second). Of course if your calculations take a long time, which blocks window updates, you won't get 60 executions of your function per second.

In the GlowScript example programs there are now two examples of the ball bouncing in a box that use the timer scheme. Do take a look.


Bruce Sherwood

unread,
Apr 21, 2013, 5:02:52 PM4/21/13
to glowscri...@googlegroups.com
I simplified these new programs somewhat and renamed them "asynchronous", which is the word used to describe programs in which the program isn't executed in a straight-line sequence of code. Rather, the program has functions that are called in response to an event or whenever time is available. Such programs are also called "event-driven" programs.

Bruce Sherwood

unread,
Apr 28, 2013, 10:50:43 PM4/28/13
to glowscri...@googlegroups.com
Yet another renaming, to "Callbacks", that being the name for a function that gets "called back" when some event happens.
Reply all
Reply to author
Forward
0 new messages