Game Development Reference
In-Depth Information
So we add the key state variables and timer as shown next:
// Timer to move the sprite
private var m_timer:Timer;
// Booleans to keep track of the key state (held down or not)
private var m_keyUP:Boolean = false;
private var m_keyDOWN:Boolean = false;
private var m_keyLEFT:Boolean = false;
private var m_keyRIGHT:Boolean = false;
In addition to the key down handling, we add the key up handling and a timer in
the constructor:
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
m_timer = new Timer(100);
m_timer.addEventListener(TimerEvent.TIMER, onTimer);
m_timer.start();
Here is the complete listing for the fast-moving sprite, with the same compiler
options for the previous implementation:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
public class FastSpriteMove extends Sprite
{
// Width and Height of the stage must be equal
// of less than what is defined in the compiler options
private static const WIDTH:int = 500;
private static const HEIGHT:int = 500;
// Determines how much to move the sprite, for
// every key down event
private static const SPEED:int = 10;
// The sprite that will be moved around
private var m_sprite:Sprite = new Sprite();
// Timer to move the sprite
private var m_timer:Timer;
// Booleans to keep track of the key state (held down or not)
private var m_keyUP:Boolean = false;
private var m_keyDOWN:Boolean = false;
 
Search WWH ::




Custom Search