Guest
Public paste!

sheel

By: a guest | Sep 29th, 2009 | Syntax: ActionScript | Size: 3.72 KB | Hits: 60 | Expires: Never
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.                         stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  34.                         stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
  35.                         handChar= new HandChar ();
  36.                         handChar.x = 200;
  37.                         handChar.y = 90;
  38.                         handChar.scaleX = 1;
  39.                         handChar.scaleX = 1;
  40.                         addChild(handChar);
  41.                        
  42.                 } // end function Main
  43.                
  44.                 private function checkKeysDown (event:KeyboardEvent):void
  45.                 { // start private function checkKeysDown
  46.                         if(event.keyCode == 37 || event.keyCode == 65)
  47.                         {
  48.                                 leftKeyDown = true;
  49.                         }
  50.                         if(event.keyCode == 38 || event.keyCode == 87)
  51.                         {
  52.                                 upKeyDown = true;
  53.                         }
  54.                         if(event.keyCode == 39 || event.keyCode == 68)
  55.                         {
  56.                                 rightKeyDown = true;
  57.                         }
  58.                         if(event.keyCode == 40 || event.keyCode == 83)
  59.                         {
  60.                                 downKeyDown = true;
  61.                         }
  62.                 } // end private function checkKeysDown
  63.                
  64.                 private function checkKeysUp (event:KeyboardEvent):void
  65.                 { // start private function checkKeysUp
  66.                         if(event.keyCode == 37 || event.keyCode == 65)
  67.                         {
  68.                                 leftKeyDown = false;
  69.                         }
  70.                         if(event.keyCode == 38 || event.keyCode == 87)
  71.                         {
  72.                                 upKeyDown = false;
  73.                         }
  74.                         if(event.keyCode == 39 || event.keyCode == 68)
  75.                         {
  76.                                 rightKeyDown = false;
  77.                         }
  78.                         if(event.keyCode == 40 || event.keyCode == 83)
  79.                         {
  80.                                 downKeyDown = false;
  81.                         }
  82.                 } // end private function checkKeysUp
  83.                        
  84.                 public function moveHand (e:Event) : void
  85.                 { // start public function moveHand
  86.                         if(leftKeyDown)
  87.                         {
  88.                                 if(upKeyDown || mainJumping)
  89.                                 {
  90.                                 handChar.x -=speed;
  91.                                 handChar.scaleX = -1;
  92.                                 handChar.gotoAndPlay(5);
  93.                                 mainJump();
  94.                                 return;
  95.                                 }
  96.                                 handChar.x -= speed;
  97.                                 handChar.scaleX = -1;
  98.                                 handChar.gotoAndPlay(1);
  99.                                 return;
  100.                         }
  101.                        
  102.                         if(rightKeyDown)
  103.                         {
  104.                                 if(upKeyDown || mainJumping)
  105.                                 {
  106.                                 handChar.x +=speed;
  107.                                 handChar.scaleX = 1;
  108.                                 handChar.gotoAndPlay(5);
  109.                                 mainJump();
  110.                                 return;
  111.                                 }
  112.                                
  113.                                 handChar.x += speed;
  114.                                 handChar.scaleX = 1;
  115.                                 handChar.gotoAndPlay(1);
  116.                                 return;
  117.                         }
  118.                        
  119.                         if(upKeyDown || mainJumping)
  120.                         {
  121.                                 handChar.gotoAndPlay(5);
  122.                                 mainJump();
  123.                                 return;
  124.                         }
  125.                         handChar.gotoAndPlay(3);
  126.                 } // end public function Movehand
  127.                
  128.                 function mainJump():void
  129.                 { // start function mainJump
  130.                         if(!mainJumping)
  131.                         {
  132.                                 mainJumping = true;
  133.                                 jumpSpeed = jumpSpeedLimit*-1;
  134.                                 handChar.y += jumpSpeed;
  135.                         }
  136.                         else
  137.                         {
  138.                         if(jumpSpeed < 0)
  139.                         {
  140.                                 jumpSpeed *= 1 - jumpSpeedLimit/75;
  141.                                 if(jumpSpeed > -jumpSpeedLimit/5)
  142.                                 {
  143.                                         jumpSpeed *= -1;
  144.                                 }
  145.                         }
  146.                         if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit)
  147.                                 {
  148.                                         jumpSpeed *= 1 + jumpSpeedLimit/50;
  149.                                 }
  150.                         handChar.y += jumpSpeed;
  151.                        
  152.                         if(handChar.y >= stage.stageHeight - handChar.height)
  153.                                 {
  154.                                 mainJumping = false;
  155.                                 handChar.y = stage.stageHeight - handChar.height;
  156.                                 }
  157.                         }
  158.                 } // end function mainJump
  159.                 } // end public class Main
  160. } // end package