Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var vx:int = 0;
- var vy:int = 0;
- var rightInnerBoundary:uint;
- var leftInnerBoundary:uint;
- var topInnerBoundary:uint;
- var bottomInnerBoundary:uint;
- var gravity:Number = 0.3;
- if(!stage){
- this.addEventListener(Event.ADDED_TO_STAGE, stageReady);
- }else{
- stageReady();
- }
- function stageReady(event:Event = null):void {
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
- stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
- stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
- rightInnerBoundary
- = (stage.stageWidth /2) + (stage.stageWidth /4);
- leftInnerBoundary
- = (stage.stageWidth /2) - (stage.stageWidth /4);
- topInnerBoundary
- = (stage.stageWidth /2) - (stage.stageWidth /4);
- bottomInnerBoundary
- = (stage.stageWidth /2) + (stage.stageWidth /4);
- }
- function keyDownHandler(event: KeyboardEvent): void
- {
- if (event.keyCode == Keyboard.LEFT)
- {
- vx = -5;
- character.gotoAndPlay("WALKL");
- } else if (event.keyCode == Keyboard.RIGHT)
- {
- vx = 5;
- character.gotoAndPlay("WALKR");
- }else if (event.keyCode == Keyboard.UP)
- {
- vy = -5;
- character.gotoAndPlay("WALKR");
- character.gravity = 0;
- }else if (event.keyCode == Keyboard.DOWN)
- {
- vy = 5;
- character.gotoAndPlay("WALKL");
- }
- }
- function keyUpHandler(event: KeyboardEvent): void
- {
- if (event.keyCode == Keyboard.LEFT)
- {
- vx = 0;
- character.gotoAndStop("IDLEL");
- character.gravity = 0.3;
- } if (event.keyCode == Keyboard.RIGHT)
- {
- vx = 0;
- character.gotoAndStop("IDLER");
- character.gravity = 0.3;
- }
- if (event.keyCode == Keyboard.UP)
- {
- vy = 0;
- character.gotoAndStop("IDLER");
- character.gravity = 0.3;
- } if (event.keyCode == Keyboard.DOWN)
- {
- vy = 0;
- character.gotoAndStop("IDLER");
- character.gravity = 0.3;
- }
- }
- function enterFrameHandler(event: Event): void
- {
- import Collision
- character.x += vx;
- character.x += vy;
- Collision.block(character, boundary);
- if (character.x < leftInnerBoundary)
- {
- character.x = leftInnerBoundary;
- background.x -= vx;
- } if (character.x + 90 > rightInnerBoundary)
- {
- character.x = rightInnerBoundary - 90;
- background.x -= vx;
- } if (character.y < topInnerBoundary)
- {
- character.y = topInnerBoundary;
- background.y -= vy;
- } if (character.y + 90 > bottomInnerBoundary)
- {
- character.y = bottomInnerBoundary - 90;
- background.y -= vy;
- }
- if (background.x > 0)
- {
- background.x = 0;
- }
- if (background.y > 0)
- {
- background.y = 0;
- }
- if (background.x < stage.stageWidth - background.width)
- {
- background.x = stage.stageWidth - background.width;
- }
- if (background.y < stage.stageHeight - background.height)
- {
- background.y = stage.stageHeight - background.height;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment