Game Development Reference
In-Depth Information
public class SpriteMove 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();
public function SpriteMove()
{
m_sprite.graphics.beginFill(0x555555);
m_sprite.graphics.drawCircle(0, 0, 25);
m_sprite.graphics.endFill();
addChild(m_sprite);
stage.addEventListener(KeyboardEvent.KEY_DOWN,
onKeyDown);
}
// The callback fired when the key is pressed down
private function onKeyDown(e:KeyboardEvent):void {
var key:uint = e.keyCode;
switch (key) {
case Keyboard.UP:
m_sprite.y -= SPEED;
// make sure it does not go negative
if ( m_sprite.y < 0 )
m_sprite.y = 0;
break;
case Keyboard.DOWN:
m_sprite.y += SPEED;
// does not go out of stage
if ( m_sprite.y > HEIGHT )
m_sprite.y = HEIGHT;
break;
case Keyboard.LEFT:
m_sprite.x -= SPEED;
// make sure it does not go negative
if ( m_sprite.x < 0 )
m_sprite.x = 0;
break;
case Keyboard.RIGHT:
// does not go out of stage
m_sprite.x += SPEED;
if ( m_sprite.x > WIDTH )
m_sprite.x = WIDTH;
 
Search WWH ::




Custom Search