Guest User

Untitled

a guest
Feb 8th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.events.Event;
  2.  
  3. //-------------------
  4. var vy:Number = 0;
  5. var gravity:Number = 2;
  6. var inAir:Boolean = true;
  7. var jumping:Boolean = false;
  8. var platforms:Array = new Array(platform1, platform2, platform3, platform4, platform5, platform6, platform7);
  9. //-------------------
  10.  
  11. stage.addEventListener(Event.ENTER_FRAME, tick);
  12.  
  13. function tick(e:Event)
  14. {
  15.     vy += gravity;
  16.    
  17.     //if hero is in the air apply gravity
  18.     if (inAir)
  19.     {
  20.         hero.y += vy;
  21.     }
  22.     //set terminal velocity - this is to prevent hero falling through platforms.
  23.     if (vy < 15)
  24.     {
  25.         vy = 15;
  26.     }
  27.    
  28.     //loop function for platforms
  29.     for (var i:int = 0; i < platforms.length; i++)
  30.     {
  31.         //perform collsion check with platforms
  32.         if (platforms[i].hitTestPoint(hero.x, hero.y, true))
  33.         {
  34.             hero.y = platforms[i].y; //set hero's point of reference to platforms
  35.             vy = 0; //disable gravity for hero when on a platform
  36.             jumping = false;
  37.             inAir = false;
  38.             break;
  39.         }
  40.         else
  41.         {
  42.             inAir = true
  43.         }
  44.     }
  45. }
Add Comment
Please, Sign In to add comment