Liberty BASIC Help Online

Pauses and Timing
 
CONTROLLING THE ANIMATION
If the frames of animation are drawn with no pause, the action may be too fast to see or control on faster computers.  A program can control the speed by drawing frames of animation at a set time interval with the TIMER, or with a simple PAUSE SUBROUTINE.
 
A REAL TIMER!
The new Liberty BASIC TIMER is perfect for use with animation.  It will cause the branch label specified to be executed at the time interval specified in milliseconds.  There are 1,000 milliseconds in one second.  A half second interval would require the milliseconds parameter to be 500.  A one-quarter second interval would require a milliseconds parameter of 250 and a one-tenth of a second interval would use a milliseconds parameter of 100.  The following example causes a frame to be drawn every one-tenth of a second:
 
TIMER 100, [DrawFrame]
 
To turn off the TIMER, use this:
 
TIMER 0
 
The TIMER can be activated or reactivated in the code as desired by repeating the TIMER command.  The time interval can be changed, as can the branch label to execute when the timer fires.  Here is an example that could occur in the same program as the TIMER example above:
 
TIMER 250, [DrawExplosion]
 
AN IMPORTANT NOTE ABOUT THE TIMER!
The cpu clock "ticks" 18 times per second.  This means that it "ticks" roughly every 56 milliseconds.  If the time interval is set to 56 milliseconds or less, the resulting animation will run at a top speed of 18 frames per second.   If a program stops for any reason, the timer ticks accumulate and the accumulated ticks fire off rapidly when the program action resumes.  This might happen when the program gives the user a notice message, for instance.  If action is to stop for any reason, it is necessary to issue a TIMER 0 command to stop the timer.  When the action is to resume, then the timer is restarted with a TIMER ms, [BranchLabel] command.
 
A SIMPLE PAUSE SUBROUTINE
On a fast computer, the frames of animation may run too quickly to be useful.  It is easy to code a simple pause between frames of animation.  The following little subroutine called "Pause" requires a parameter for the number of milliseconds to pause between frames.  There are 1,000 milliseconds in one second.  A half second pause requires the mil parameter to be 500.  A one-quarter second pause requires a mil parameter of 250 and a one-tenth of a second pause uses a mil parameter of 100.
 
sub Pause mil
    t=time$("milliseconds")
    while time$("milliseconds")
    wend
    end sub
 
This code will activate a one-tenth of a second pause between frames, when used after each DRAWSPRITES command:
 
    call Pause 100
 
A processing loop for a sprite animation with pauses between frames might look like this:
 
[loop]
    scan
    call Pause 100
    print #w.g, "setfocus; when characterInput [quit]"
    print #w.g, "when leftButtonDown [left]"
    print #w.g, "when rightButtonDown [right]"
 
    gosub [drawFrame]
    goto [loop]
 
TIMER VS PAUSE
The TIMER causes a branch label to be executed at a set interval, which is measured in milliseconds.  Setting this interval to 250 (for example) will cause the branch label to be executed each one-quarter second.  A PAUSE is just that.  It causes the action to pause for the set interval.  Pausing for one-quarter second BETWEEN frames of animation will not be the same as drawing a frame of animation every one-quarter second, because in addition to the one-quarter second pause, time is taken while the code at the branch label is executed.  Using the TIMER results in much smoother and more accurate timing for animation.
 
See how to Add a Mask to sprites.
 
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/