package { import flash.display.MovieClip import flash.events.Event import flash.events.KeyboardEvent public class DocumentMain extends MovieClip { public var _startMarker:StartMarker; public var _player:Player; public var _boundaries:Boundaries; private var _vx:Number; private var _vy:Number; public function DocumentMain():void { // assign default values _vx = 0; _vy = 0; _startMarker.visible = false; // set focus for keyboard input stage.focus = stage; // add event listeners this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); } private function enterFrameHandler(e:Event):void { // gravitate the player _vy += 2; // move the player _player.x += _vx; _player.y += _vy; // process collisions processCollisions(); // scroll the stage scrollStage(); } private function keyDownHandler(e:KeyboardEvent):void { switch (e.keyCode) { case 37: _vx = -7; break; case 38: _vy = -20; break; case 39: _vx = 7; break; default: } } private function keyUpHandler(e:KeyboardEvent):void { switch (e.keyCode) { case 37: case 39: _vx = 0; break; default: } } private function processCollisions():void { // when player is falling if (_vy > 0) { // respawn if player fell off the stage if (_player.y > stage.stageHeight) { _player.x = _startMarker.x; _player.y = _startMarker.y; _boundaries.x = 0; _boundaries.y = 0; _vy = 0; } // otherwise, process collisions with boundaries else { var collision:Boolean = false; if (_boundaries.hitTestPoint(_player.x, _player.y, true)) { collision = true; } if (collision) { while (collision) { _player.y -= 0.1; collision = false; if (_boundaries.hitTestPoint(_player.x, _player.y, true)) { collision = true; } } _vy = 0; } } } } private function scrollStage():void { _boundaries.x += (stage.stageWidth * 0.5) - _player.x; _player.x = stage.stageWidth * 0.5; } } }