Guest User

Untitled

a guest
Apr 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.MovieClip;
  4.     import flash.events.KeyboardEvent;
  5.     import flash.ui.Keyboard;
  6.     import flash.events.Event;
  7.    
  8.     public class Main extends MovieClip
  9.     {
  10.         var vx:int;
  11.         var vy:int;
  12.         var rightInnerBoundary:uint;
  13.         var leftInnerBoundary:uint;
  14.         var topInnerBoundary:uint;
  15.         var bottomInnerBoundary:uint;
  16.        
  17.         public function Main()
  18.         {
  19.             init();
  20.         }
  21.         function init():void
  22.         {
  23.             vx = 0;
  24.             vy = 0;
  25.            
  26.             rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4);
  27.             leftInnerBoundary = (stage.stageWidth / 2) - (stage.stageWidth / 4);
  28.             topInnerBoundary = (stage.stageHeight / 2) - (stage.stageHeight / 4);
  29.             bottomInnerBoundary = (stage.stageHeight / 2) + (stage.stageHeight / 4);
  30.            
  31.             stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  32.             stage.addEventListener(KeyboardEvent.KEY_UP, onKeyNotPressed);
  33.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  34.         }
  35.         function onKeyPressed(event:KeyboardEvent):void
  36.         {
  37.             if (event.keyCode == Keyboard.LEFT)
  38.             {
  39.                 vx = -10;
  40.             }
  41.             else if (event.keyCode == Keyboard.RIGHT)
  42.             {
  43.                 vx = 10;
  44.             }
  45.             else if (event.keyCode == Keyboard.UP)
  46.             {
  47.                 vy = -10;
  48.             }
  49.             else if (event.keyCode == Keyboard.DOWN)
  50.             {
  51.                 vy = 10;
  52.             }
  53.         }
  54.         function onKeyNotPressed(event:KeyboardEvent):void
  55.         {
  56.             if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
  57.             {
  58.                 vx = 0;
  59.             }
  60.             else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
  61.             {
  62.                 vy = 0;
  63.             }
  64.         }
  65.         function onEnterFrame(event:Event):void
  66.         {
  67.             var playerHalfWidth:uint = player.width / 2;
  68.             var playerHalfHeight:uint = player.height / 2;
  69.             var backgroundHalfWidth:uint = background.width / 2;
  70.             var backgroundHalfHeight:uint = background.height / 2;
  71.            
  72.             player.x += vx;
  73.             player.y += vy;
  74.            
  75.             if (player.x - playerHalfWidth < leftInnerBoundary)
  76.             {
  77.                 player.x = leftInnerBoundary + playerHalfWidth;
  78.                 background.x -= vx;
  79.             }
  80.             else if (player.x + playerHalfWidth > rightInnerBoundary)
  81.             {
  82.                 player.x = rightInnerBoundary - playerHalfWidth;
  83.                 background.x -= vx;
  84.             }
  85.             if (player.y - playerHalfHeight < topInnerBoundary)
  86.             {
  87.                 player.y = topInnerBoundary + playerHalfHeight;
  88.                 background.y -= vy;
  89.             }
  90.             else if (player.y + playerHalfHeight > bottomInnerBoundary)
  91.             {
  92.                 player.y = bottomInnerBoundary - playerHalfHeight;
  93.                 background.y -= vy;
  94.             }
  95.        
  96.        
  97.         if (background.x + backgroundHalfWidth < stage.stageWidth)
  98.         {
  99.             background.x = stage.stageWidth - backgroundHalfWidth;
  100.         }
  101.         else if (background.x - backgroundHalfWidth > 0)
  102.         {
  103.             background.x = 0 + backgroundHalfWidth;
  104.         }
  105.         if (background.y - backgroundHalfHeight > 0)
  106.         {
  107.             background.y = 0 + backgroundHalfHeight;
  108.         }
  109.         else if (background.y + backgroundHalfHeight < stage.stageHeight)
  110.         {
  111.             background.y = stage.stageHeight - backgroundHalfHeight;
  112.         }
  113.            
  114.         }
  115.     }
  116. }
Add Comment
Please, Sign In to add comment