package { import flash.display.Bitmap; import flash.display.Sprite; import flash.media.Sound; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.geom.Point; import flash.geom.Rectangle; import flash.utils.getTimer; import flash.utils.Timer; public class LDEmbedTest extends Sprite { public static const KEY_LEFT:int = 37; public static const KEY_RIGHT:int = 39; public static const KEY_JUMP:int = 32; public static const UPDATE_INTERVAL:Number = 1 / 60; public static const ACCELERATION_SPEED_GROUND:Number = 0.5; public static const ACCELERATION_SPEED_AIR:Number = 0.125; public static const DECELERATION_SPEED_GROUND:Number = 1.0; public static const DECELERATION_SPEED_AIR:Number = 0.125; public static const MAX_SPEED:Number = 8; public static const GRAVITY:Number = 0.3; [Embed(source="resources/player.png")] private var playerBitmapEmbed:Class; [Embed(source="resources/coin.png")] private var coinBitmapEmbed:Class; [Embed(source="resources/jump.mp3")] private var jumpSoundEmbed:Class; [Embed(source="resources/coin.mp3")] private var coinSoundEmbed:Class; private var playerBitmap:Bitmap; private var coinBitmap:Bitmap; private var jumpSound:Sound; private var coinSound:Sound; private var moveLeftKeyDown:Boolean, moveRightKeyDown:Boolean; private var moveX:int; private var velocity:Point; private var playerPosition:Point; private var onGround:Boolean; private var timer:Timer; private var lastTime:Number = 0; private var slop:Number = 0; public function LDEmbedTest() { coinBitmap = new coinBitmapEmbed(); coinBitmap.x = 150; coinBitmap.y = stage.stageHeight - 250 - coinBitmap.height; addChild(coinBitmap); playerBitmap = new playerBitmapEmbed(); playerBitmap.x = 10; playerBitmap.y = stage.stageHeight - 10 - playerBitmap.height; addChild(playerBitmap); coinSound = new coinSoundEmbed(); jumpSound = new jumpSoundEmbed(); velocity = new Point(0, 0); playerPosition = new Point(10, 10); stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); lastTime = getCurrentTime(); stage.frameRate = 60; timer = new Timer(1000 / 60); timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); } public static function getCurrentTime():Number { return getTimer() / 1000.0; } private function onMouseDown(event:MouseEvent):void { } private function onKeyDown(event:KeyboardEvent):void { switch (event.keyCode) { case KEY_LEFT: moveLeftKeyDown = true; moveX = -1; break; case KEY_RIGHT: moveRightKeyDown = true; moveX = 1; break; case KEY_JUMP: if (onGround) { jumpSound.play(); onGround = false; velocity.y = 10; } break; } } private function onKeyUp(event:KeyboardEvent):void { switch (event.keyCode) { case KEY_LEFT: moveLeftKeyDown = false; if (moveX == -1) { if (moveRightKeyDown) { moveX = 1; } else { moveX = 0; } } break; case KEY_RIGHT: moveRightKeyDown = false; if (moveX == 1) { if (moveLeftKeyDown) { moveX = -1; } else { moveX = 0; } } break; } } private function onTimer(event:TimerEvent):void { var currentTime:Number = getCurrentTime(); var elapsedTime:Number = currentTime - lastTime + slop; while (elapsedTime > UPDATE_INTERVAL) { updateSimulation(); elapsedTime -= UPDATE_INTERVAL; } slop = elapsedTime; lastTime = currentTime; } private function updateSimulation():void { if (velocity.x > 0 && moveX <= 0) { velocity.x -= onGround ? DECELERATION_SPEED_GROUND : DECELERATION_SPEED_AIR; if (velocity.x < 0) { velocity.x = 0; } } else if (velocity.x < 0 && moveX >= 0) { velocity.x += onGround ? DECELERATION_SPEED_GROUND : DECELERATION_SPEED_AIR; if (velocity.x > 0) { velocity.x = 0; } } if (moveX < 0) { velocity.x -= onGround ? ACCELERATION_SPEED_GROUND : ACCELERATION_SPEED_AIR; if (velocity.x < -MAX_SPEED) { velocity.x = -MAX_SPEED; } } else if (moveX > 0) { velocity.x += onGround ? ACCELERATION_SPEED_GROUND : ACCELERATION_SPEED_AIR; if (velocity.x > MAX_SPEED) { velocity.x = MAX_SPEED; } } velocity.y -= GRAVITY; onGround = false; playerPosition.x += velocity.x; playerPosition.y += velocity.y; if (playerPosition.y < 10) { playerPosition.y = 10; onGround = true; } playerBitmap.x = playerPosition.x; playerBitmap.y = stage.stageHeight - playerPosition.y - playerBitmap.height; if (contains(coinBitmap) && coinBitmap.getRect(stage).intersects(playerBitmap.getRect(stage))) { coinSound.play(); removeChild(coinBitmap); } } } }