Advertisement
tytbone

Untitled

Jun 23rd, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //These variables will note which keys are down
  2. //We don't need the up or down key just yet
  3. //but we will later
  4.  
  5. var leftKeyDown:Boolean = false;
  6. var upKeyDown:Boolean = false;
  7. var rightKeyDown:Boolean = false;
  8. var downKeyDown:Boolean = false;
  9. //the main speed of the character
  10. var mainSpeed:Number = 7;
  11. trace("variables are good");
  12.  
  13.  
  14. //adding a listener to mcMain which will make it move
  15. //based on the key strokes that are down
  16.  
  17. main_mc.addEventListener(Event.ENTER_FRAME, moveChar);
  18. function moveChar(event:Event):void{
  19.     //if certain keys are down then move the character
  20.     if (leftKeyDown) {
  21.         main_mc.x -= mainSpeed;
  22.     }
  23.     if (rightKeyDown) {
  24.         main_mc.y += mainSpeed;
  25.     }
  26.     if (upKeyDown || mainJumping) {
  27.     // "||" means OR
  28.         mainJump();
  29.     }
  30. }
  31.  
  32. trace("moveChar is good");
  33.        
  34. //listening for the keystrokes
  35. //this listener will listen for down keystrokes
  36. stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  37. function checkKeysDown(event:KeyboardEvent):void{
  38.     //making the booleans true based on the keycode
  39.     //WASD Keys or arrow keys
  40.     if(event.keyCode == 37 || event.keyCode == 65) {
  41.         leftKeyDown = true;
  42.     }
  43.     if(event.keyCode == 38 || event.keyCode == 87) {
  44.         upKeyDown = true;
  45.     }
  46.     if(event.keyCode == 39 || event.keyCode == 68) {
  47.         rightKeyDown = true;
  48.     }
  49.     if(event.keyCode == 40 || event.keyCode == 83) {
  50.         downKeyDown = true;
  51.     }
  52. }
  53. trace("checkKeysDown is good");
  54.  
  55. //this listener will listen for keys being released
  56. stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
  57. function checkKeysUp(event:KeyboardEvent):void{
  58.     //making the booleans false based on the keycode
  59.     if(event.keyCode == 37 || event.keyCode == 65) {
  60.         leftKeyDown = false;
  61.     }
  62.     if(event.keyCode == 38 || event.keyCode == 87) {
  63.         upKeyDown = false;
  64.     }
  65.     if(event.keyCode == 39 || event.keyCode == 68) {
  66.         rightKeyDown = false;
  67.     }
  68.     if(event.keyCode == 40 || event.keyCode == 83) {
  69.         downKeyDown = false;
  70.     }
  71. }
  72. trace("checkKeysUp is good");
  73.  
  74.  
  75. //whether or not the main guy is jumping
  76. var mainJumping:Boolean = false;
  77. //how quickly should the jump start off
  78. var jumpSpeedLimit:int = 15;
  79. //the current speed of the jump;
  80. var jumpSpeed:Number = jumpSpeedLimit;
  81.     trace("variables 2 is good");
  82.  
  83. //jumping function
  84. function mainJump():void{
  85.     //if main isn't already jumping
  86.     if(!mainJumping){
  87.         /*putting an exclamation mark in front of a
  88.         boolean inverts the condition. An exclamation
  89.         means 'not'. This statement says:
  90.         "if not mainJumping". Or rather, "if not true".
  91.         So the results of this statement is false.*/
  92.        
  93.         //then start jumping
  94.         mainJumping = true;
  95.         jumpSpeed = jumpSpeedLimit*-1;
  96.         main_mc.y += jumpSpeed;
  97.     } else {
  98.         //then continue jumping if already in the air
  99.         //crazy math that I won't explain
  100.         if(jumpSpeed < 0){
  101.             jumpSpeed *= 1 - jumpSpeedLimit/75;
  102.             if(jumpSpeed > -jumpSpeedLimit/5){
  103.                 jumpSpeed *= -1;
  104.             }
  105.         }
  106.         if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit) {
  107.             jumpSpeed *= 1 + jumpSpeedLimit/50;
  108.         }
  109.         main_mc.y += jumpSpeed;
  110.         //if main hits the floor, then stop jumping
  111.         //of course, we'll change this once we create the level
  112.         if(main_mc.y >= stage.stageHeight - main_mc.height) {
  113.             mainJumping = false;
  114.             main_mc.y = stage.stageHeight - main_mc.height;
  115.         }
  116.     }
  117. }
  118. trace("mainJump is good");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement