Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.MovieClip;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- import flash.events.Event;
- public class Main extends MovieClip
- {
- var vx:int;
- var vy:int;
- var rightInnerBoundary:uint;
- var leftInnerBoundary:uint;
- var topInnerBoundary:uint;
- var bottomInnerBoundary:uint;
- public function Main()
- {
- init();
- }
- function init():void
- {
- vx = 0;
- vy = 0;
- rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
- leftInnerBoundary = (stage.stageWidth / 2) - (stage.stageWidth / 4);
- topInnerBoundary = (stage.stageHeight / 2) - (stage.stageHeight / 4);
- bottomInnerBoundary = (stage.stageHeight / 2) + (stage.stageHeight / 4);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
- stage.addEventListener(KeyboardEvent.KEY_UP, onKeyNotPressed);
- addEventListener(Event.ENTER_FRAME, onEnterFrame);
- }
- function onKeyPressed(event:KeyboardEvent):void
- {
- if (event.keyCode == Keyboard.LEFT)
- {
- vx = -10;
- }
- else if (event.keyCode == Keyboard.RIGHT)
- {
- vx = 10;
- }
- else if (event.keyCode == Keyboard.UP)
- {
- vy = -10;
- }
- else if (event.keyCode == Keyboard.DOWN)
- {
- vy = 10;
- }
- }
- function onKeyNotPressed(event:KeyboardEvent):void
- {
- if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
- {
- vx = 0;
- }
- else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
- {
- vy = 0;
- }
- }
- function onEnterFrame(event:Event):void
- {
- var playerHalfWidth:uint = player.width / 2;
- var playerHalfHeight:uint = player.height / 2;
- var backgroundHalfWidth:uint = background.width / 2;
- var backgroundHalfHeight:uint = background.height / 2;
- player.x += vx;
- player.y += vy;
- if (player.x - playerHalfWidth < leftInnerBoundary)
- {
- player.x = leftInnerBoundary + playerHalfWidth;
- background.x -= vx;
- }
- else if (player.x + playerHalfWidth > rightInnerBoundary)
- {
- player.x = rightInnerBoundary - playerHalfWidth;
- background.x -= vx;
- }
- if (player.y - playerHalfHeight < topInnerBoundary)
- {
- player.y = topInnerBoundary + playerHalfHeight;
- background.y -= vy;
- }
- else if (player.y + playerHalfHeight > bottomInnerBoundary)
- {
- player.y = bottomInnerBoundary - playerHalfHeight;
- background.y -= vy;
- }
- if (background.x + backgroundHalfWidth < stage.stageWidth)
- {
- background.x = stage.stageWidth - backgroundHalfWidth;
- }
- else if (background.x - backgroundHalfWidth > 0)
- {
- background.x = 0 + backgroundHalfWidth;
- }
- if (background.y - backgroundHalfHeight > 0)
- {
- background.y = 0 + backgroundHalfHeight;
- }
- else if (background.y + backgroundHalfHeight < stage.stageHeight)
- {
- background.y = stage.stageHeight - backgroundHalfHeight;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment