Share Pastebin
Guest
Public paste!

sheel

By: a guest | Sep 30th, 2009 | Syntax: ActionScript | Size: 4.88 KB | Hits: 18 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. package
  2. { // start package
  3.        
  4.         import flash.display.MovieClip;
  5.         import flash.events.*;
  6.        
  7.         public class Main extends MovieClip
  8.         { // start public class Main
  9.                 var speed:Number = 5;
  10.                
  11.                 var handChar:HandChar;
  12.                 // define MovieClip "handChar"
  13.                
  14.                 var leftKeyDown:Boolean = false;
  15.                 var upKeyDown:Boolean = false;
  16.                 var rightKeyDown:Boolean = false;
  17.                 var downKeyDown:Boolean = false;
  18.                 // define keys left, right, up, down - for movement (left, right, up, may add down)
  19.                
  20.                 var hKeyDown:Boolean = false;
  21.                 var jKeyDown:Boolean = false;
  22.                 var kKeyDown:Boolean = false;
  23.                 //define keys h, j, k - for attacks (rock, paper, scissor)
  24.                
  25.                 var mainJumping:Boolean = false;
  26.                 var jumpSpeedLimit:int = 15;
  27.                 var jumpSpeed:Number = jumpSpeedLimit;
  28.                 // define jump
  29.                
  30.                 public function Main()
  31.                 { // start function Main
  32.                         addEventListener(Event.ENTER_FRAME, moveHand);
  33.                         addEventListener(Event.ENTER_FRAME, attackHand);
  34.                         addEventListener(Event.ENTER_FRAME, boundaries);
  35.                         stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  36.                         stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
  37.                         handChar= new HandChar ();
  38.                         handChar.x = 200;
  39.                         handChar.y = 90;
  40.                         handChar.scaleX = 1;
  41.                         handChar.scaleX = 1;
  42.                         addChild(handChar);
  43.                        
  44.                 } // end function Main
  45.                
  46.                 private function checkKeysDown (event:KeyboardEvent):void
  47.                 { // start private function checkKeysDown
  48.                         if(event.keyCode == 37 || event.keyCode == 65)
  49.                         {
  50.                                 leftKeyDown = true;
  51.                         }
  52.                         if(event.keyCode == 38 || event.keyCode == 87)
  53.                         {
  54.                                 upKeyDown = true;
  55.                         }
  56.                         if(event.keyCode == 39 || event.keyCode == 68)
  57.                         {
  58.                                 rightKeyDown = true;
  59.                         }
  60.                         if(event.keyCode == 40 || event.keyCode == 83)
  61.                         {
  62.                                 downKeyDown = true;
  63.                         }
  64.                         if(event.keyCode == 72)
  65.                         {
  66.                                 hKeyDown = true;
  67.                         }
  68.                         if(event.keyCode == 74)
  69.                         {
  70.                                 jKeyDown = true;
  71.                         }
  72.                         if(event.keyCode == 75)
  73.                         {
  74.                                 kKeyDown = true;
  75.                         }
  76.                 } // end private function checkKeysDown
  77.                
  78.                 private function checkKeysUp (event:KeyboardEvent):void
  79.                 { // start private function checkKeysUp
  80.                         if(event.keyCode == 37 || event.keyCode == 65)
  81.                         {
  82.                                 leftKeyDown = false;
  83.                         }
  84.                         if(event.keyCode == 38 || event.keyCode == 87)
  85.                         {
  86.                                 upKeyDown = false;
  87.                         }
  88.                         if(event.keyCode == 39 || event.keyCode == 68)
  89.                         {
  90.                                 rightKeyDown = false;
  91.                         }
  92.                         if(event.keyCode == 40 || event.keyCode == 83)
  93.                         {
  94.                                 downKeyDown = false;
  95.                         }
  96.                         if(event.keyCode == 72)
  97.                         {
  98.                                 hKeyDown = false;
  99.                         }
  100.                         if(event.keyCode == 74)
  101.                         {
  102.                                 jKeyDown = false;
  103.                         }
  104.                         if(event.keyCode == 75)
  105.                         {
  106.                                 kKeyDown = false;
  107.                         }
  108.                 } // end private function checkKeysUp
  109.                        
  110.                 public function moveHand (e:Event) : void
  111.                 { // start public function moveHand
  112.                         if(leftKeyDown)
  113.                         {
  114.                                 if(upKeyDown || mainJumping)
  115.                                 {
  116.                                         handChar.x -=speed;
  117.                                         handChar.scaleX = -1;
  118.                                         handChar.gotoAndPlay(5);
  119.                                         mainJump();
  120.                                         return;
  121.                                 }
  122.                                 handChar.x -= speed;
  123.                                 handChar.scaleX = -1;
  124.                                 handChar.gotoAndPlay(1);
  125.                                 return;
  126.                         }
  127.                        
  128.                         if(rightKeyDown)
  129.                         {
  130.                                 if(upKeyDown || mainJumping)
  131.                                 {
  132.                                         handChar.x +=speed;
  133.                                         handChar.scaleX = 1;
  134.                                         handChar.gotoAndPlay(5);
  135.                                         mainJump();
  136.                                         return;
  137.                                 }
  138.                                 handChar.x += speed;
  139.                                 handChar.scaleX = 1;
  140.                                 handChar.gotoAndPlay(1);
  141.                                 return;
  142.                         }
  143.                        
  144.                         if(upKeyDown || mainJumping)
  145.                         {
  146.                                 handChar.gotoAndPlay(5);
  147.                                 mainJump();
  148.                                 return;
  149.                         }
  150.                        
  151.                         handChar.gotoAndPlay(3);
  152.                 } // end public function Movehand
  153.                
  154.                 public function attackHand (e:Event) : void
  155.                 { /*// start public function attackHand
  156.                         if(hKeyDown)
  157.                         {
  158.                                 handChar.gotoAndStop(rock);
  159.                                 return;
  160.                         }
  161.                        
  162.                         if(jKeyDown)
  163.                         {
  164.                                 handChar.gotoAndStop(paper);
  165.                                 return;
  166.                         }
  167.                        
  168.                         if(kKeyDown)
  169.                         {
  170.                                 handChar.gotoAndStop(scissor);
  171.                                 return;
  172.                         } */
  173.                 } // end public function attackHand
  174.                
  175.                 function boundaries (e:Event) : void
  176.                  { // start function boundaries
  177.                          
  178.                          if(handChar.x > stage.stageWidth - handChar.width/2)
  179.                          {
  180.                                  handChar.x = stage.stageWidth - handChar.width/2;
  181.                          }
  182.                          if(handChar.x < 15)
  183.                          {
  184.                                  handChar.x = 15;
  185.                          }
  186.          } // end function boundaries
  187.                        
  188.                
  189.                 function mainJump():void
  190.                 { // start function mainJump
  191.                         if(!mainJumping)
  192.                         {
  193.                                 mainJumping = true;
  194.                                 jumpSpeed = jumpSpeedLimit*-1;
  195.                                 handChar.y += jumpSpeed;
  196.                         }
  197.                         else
  198.                         {
  199.                         if(jumpSpeed < 0)
  200.                         {
  201.                                 jumpSpeed *= 1 - jumpSpeedLimit/75;
  202.                         if(jumpSpeed > -jumpSpeedLimit/5)
  203.                                 {
  204.                                         jumpSpeed *= -1;
  205.                                 }
  206.                         }
  207.                         if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
  208.                                 {
  209.                                         jumpSpeed *= 1 + jumpSpeedLimit/50;
  210.                                 }
  211.                         handChar.y += jumpSpeed;
  212.                        
  213.                         if(handChar.y >= stage.stageHeight - handChar.height)
  214.                                 {
  215.                                 mainJumping = false;
  216.                                 handChar.y = stage.stageHeight - handChar.height;
  217.                                 }
  218.                         }
  219.                 } // end function mainJump
  220.                 } // end public class Main
  221. } // end package