Share Pastebin
Guest
Public paste!

Sheel

By: a guest | Oct 14th, 2009 | Syntax: ActionScript | Size: 4.88 KB | Hits: 64 | 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 = new HandChar(); // define MovieClip "handChar"
  12.                
  13.                 var leftKeyDown:Boolean = false; // move left
  14.                 var upKeyDown:Boolean = false; // jump
  15.                 var rightKeyDown:Boolean = false; // move right
  16.                
  17.                 var hKeyDown:Boolean = false; // scissor attack
  18.                 var jKeyDown:Boolean = false; // paper attack
  19.                 var kKeyDown:Boolean = false; // rock attack
  20.                
  21.                 var mainJumping:Boolean = false;
  22.                 var jumpSpeedLimit:int = 10;
  23.                 var jumpSpeed:Number = 0;
  24.                
  25.                 public function Main()
  26.                 { // start public function Main (constructor)
  27.                         addEventListener(Event.ENTER_FRAME, moveHand);
  28.                         addEventListener(Event.ENTER_FRAME, boundaries);
  29.                         stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  30.                         stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
  31.                         handChar.handCharobject.addEventListener (Event.ENTER_FRAME, endKeyDown);
  32.                        
  33.                         createHand();
  34.                         } // end public function Main (constructor)
  35.                
  36.                 private function createHand():void
  37.                 { // start private function createHand
  38.                         handChar.x = 200;
  39.                         handChar.y = 145;
  40.                         handChar.scaleX = 1;
  41.                         handChar.scaleX = 1;
  42.                         addChild(handChar);
  43.                        
  44.                 } // end private function createHand
  45.                
  46.                 private function endKeyDown (event:Event):void
  47.                 {
  48.                         if(handChar.handCharobject.currentLabel == "endofclip")
  49.                         {
  50.                                 hKeyDown = false;
  51.                                 stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  52.                         }
  53.                         if(handChar.handCharobject.currentLabel == "endofclip")
  54.                         {
  55.                                 jKeyDown = false;
  56.                                 stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  57.                         }
  58.                         if(handChar.handCharobject.currentLabel == "endofclip")
  59.                         {
  60.                                 kKeyDown = false;
  61.                                 stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  62.                         }
  63.                 }
  64.                
  65.                 private function checkKeysDown (event:KeyboardEvent):void
  66.                 { // start private function checkKeysDown
  67.                         if(event.keyCode == 37 || event.keyCode == 65)
  68.                         {
  69.                                 leftKeyDown = true;
  70.                         }
  71.                         if(event.keyCode == 38 || event.keyCode == 87)
  72.                         {
  73.                                 upKeyDown = true;
  74.                         }
  75.                         if(event.keyCode == 39 || event.keyCode == 68)
  76.                         {
  77.                                 rightKeyDown = true;
  78.                         }
  79.                         if(event.keyCode == 72)
  80.                         {
  81.                                 hKeyDown = true;
  82.                                 stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  83.                         }
  84.                         if(event.keyCode == 74)
  85.                         {
  86.                                 jKeyDown = true;
  87.                                 stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  88.                         }
  89.                         if(event.keyCode == 75)
  90.                         {
  91.                                 kKeyDown = true;
  92.                                 stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  93.                         }
  94.                 } // end private function checkKeysDown
  95.                
  96.                 private function checkKeysUp (event:KeyboardEvent):void
  97.                 { // start private function checkKeysUp
  98.                         if(event.keyCode == 37 || event.keyCode == 65) // Right Arrow or D key
  99.                         {
  100.                                 leftKeyDown = false;
  101.                         }
  102.                         if(event.keyCode == 38 || event.keyCode == 87) //  Up Arrow or W key
  103.                         {
  104.                                 upKeyDown = false;
  105.                         }
  106.                         if(event.keyCode == 39 || event.keyCode == 68) // Left Arrow or A key
  107.                         {
  108.                                 rightKeyDown = false;
  109.                         }
  110.                 } // end private function checkKeysUp
  111.                        
  112.                 private function moveHand (e:Event) : void
  113.                 { // start private function moveHand
  114.                         var idle:Boolean = true;
  115.                        
  116.                         if(upKeyDown)
  117.                         {
  118.                                 if (!mainJumping)
  119.                                 {
  120.                                 handChar.gotoAndStop(5);
  121.                                 jumpSpeed=jumpSpeedLimit;
  122.                                 mainJumping = true;
  123.                                 }
  124.                                 idle = false;
  125.                         }
  126.                        
  127.                         if(leftKeyDown && !mainJumping)
  128.                         {
  129.                                 handChar.x -= speed;
  130.                                 handChar.scaleX = -1;
  131.                                 handChar.gotoAndPlay(1);
  132.                                 idle = false;
  133.                         }
  134.                
  135.                         if(rightKeyDown && !mainJumping)
  136.                         {
  137.                                 handChar.x += speed;
  138.                                 handChar.scaleX = 1;
  139.                                 handChar.gotoAndPlay(1);
  140.                                 idle = false;
  141.                         }
  142.                
  143.                        
  144.                
  145.                         if(hKeyDown)
  146.                         {
  147.                                 handChar.gotoAndStop("scissor");
  148.                                 idle = false;
  149.                         }
  150.                
  151.                         if(jKeyDown)
  152.                         {
  153.                                 handChar.gotoAndStop("paper");
  154.                                 idle = false;
  155.                         }
  156.                
  157.                         if(kKeyDown)
  158.                         {
  159.                                 handChar.gotoAndStop("rock");
  160.                                 idle = false;
  161.                         }
  162.                
  163.                         if(mainJumping)
  164.                         {
  165.                                 if(leftKeyDown)
  166.                                         {
  167.                                                 handChar.x -= speed;
  168.                                                 handChar.scaleX = -1;
  169.                                         }
  170.                                 if(rightKeyDown)
  171.                                         {
  172.                                                 handChar.x += speed;
  173.                                                 handChar.scaleX = 1;
  174.                                         }
  175.                                 idle = false;
  176.                         }
  177.                        
  178.                         if(idle)handChar.gotoAndPlay(3);
  179.                        
  180.                        
  181.                         handChar.y -= jumpSpeed;
  182.                         jumpSpeed -= 2;
  183.                        
  184.                         while(Ground.hitTestPoint(handChar.x, handChar.y-3, true))
  185.                         {
  186.                                 handChar.y -= 1;
  187.                                 jumpSpeed = 0;
  188.                                 mainJumping = false;
  189.                         }
  190.                 } // end private function Movehand
  191.                
  192.                 private function boundaries (e:Event):void
  193.                  { // start private function boundaries
  194.                          if(handChar.x > stage.stageWidth - handChar.width/2)
  195.                          {
  196.                                  handChar.x = stage.stageWidth - handChar.width/2;
  197.                          }
  198.                          if(handChar.x < 15)
  199.                          {
  200.                                  handChar.x = 15;
  201.                          }
  202.          } // end private function boundaries
  203.         }
  204. } // end package