Guest User

Untitled

a guest
Aug 24th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var player = 1;
  2. var gravity = 2;
  3. var speed = 10;
  4.  
  5. var plrY = new Array(0, 0, 0);
  6. var plrDir = new Array(0, 0, 0);
  7. var plrIsFalling = new Array(0, 1, 1);
  8.  
  9. var KeyListener = new Object();
  10. Key.addListener(KeyListener);
  11.  
  12. //Key controls
  13. KeyListener.onKeyDown = function()
  14. {
  15.     //Jump
  16.     if (Key.isDown(87))
  17.     {
  18.         //if the player isnt already jumping
  19.         if (plrIsFalling[player] == false)
  20.         {
  21.             //player is now jumping
  22.             plrIsFalling[player] = true;
  23.             //sets base speed to launch into air
  24.             plrY[player] = -20;
  25.             //moves off of the surface to avoid collision issues
  26.             _root["plr" + player]._y--;
  27.         }
  28.     }
  29.    
  30.     //Left
  31.     if (Key.isDown(65))
  32.     {
  33.         plrDir[player] = -1;
  34.     }
  35.    
  36.     //Right
  37.     if (Key.isDown(68))
  38.     {
  39.         plrDir[player] = 1;
  40.     }
  41.    
  42.     //Player swap button
  43.     if(Key.isDown(81)){
  44.        if(player == 1){
  45.            player = 2
  46.        }else{
  47.            player = 1
  48.        }
  49.    
  50.     }
  51. };
  52.  
  53. KeyListener.onKeyUp = function()
  54. {
  55.     //These just reset the players direction when
  56.     //the button is lifted up. Reversed to remove
  57.     //priority glitch
  58.     if (Key.isDown(68))
  59.     {
  60.         plrDir[player] = 1;
  61.  
  62.     }
  63.     else if (Key.isDown(65))
  64.     {
  65.         plrDir[player] = -1;
  66.     }
  67.     else
  68.     {
  69.         plrDir[player] = 0;
  70.     }
  71. };
  72.  
  73. //Moves the currently controlled player and runs basic collision control
  74. movePlr = function ()
  75. {
  76.     //Moves currently enabled player
  77.     _root["plr" + player]._x += plrDir[player] * speed;
  78.    
  79.     //Checks edge and wall collisions for each player
  80.     checkEdge(plr1, 1)
  81.     checkWall(plr1, 1)
  82.    
  83.     checkEdge(plr2, 2)
  84.     checkWall(plr2, 2)
  85. };
  86.  
  87. //Checks if the item has fallen off the edge of a platform
  88. checkEdge = function(item, ID){
  89.     if(item.hitF(bottom) == false){
  90.         plrIsFalling[ID] = true
  91.     }
  92. }
  93.  
  94. //Checks if item has ran into a wall
  95. checkWall = function(item, ID){
  96.     //Its dependent on the direction the item is currently moving
  97.     switch(plrDir[ID]){
  98.         case -1:
  99.             if(item.hitF(left)){
  100.                 //Use another function to allow recursion
  101.                 shiftPlr(item, -1);
  102.             }
  103.             break;
  104.         case 1:
  105.             if(item.hitF(right)){
  106.                 shiftPlr(item, 1);
  107.             }
  108.     }
  109. }
  110.  
  111. //Shifts player if they're colliding with the wall
  112. shiftPlr = function(item, dir){
  113.     //Moves them in the opposite direction to the way that they're walking
  114.     item._x += dir * -1
  115.     switch(dir){
  116.         case -1:
  117.             //If its still colliding
  118.             if(item.hitF(left)){
  119.                 //RECUUURRRRSSSIIIOOOOON
  120.                 shiftPlr(item, -1);
  121.             //if not
  122.             }else{
  123.                 //shift it back by one so they dont get stuck
  124.                 item._x += dir * -1
  125.             }
  126.             break;
  127.         case 1:
  128.             if(item.hitF(right)){
  129.                 shiftPlr(item, 1);
  130.             }else{
  131.                 item._x += dir
  132.             }
  133.             break;
  134.     }
  135. }
  136.  
  137. //Deals with vertical movement and collisions
  138. falling = function ()
  139. {
  140.     if (plrIsFalling[1] == true)
  141.     {
  142.         //Basic collision check
  143.         if (plr1.hitF(bottom) == false)
  144.         {
  145.             //If it hasnt hit the ground, increase the velocity and move
  146.             plrY[1] += gravity;
  147.             plr1._y += plrY[1];
  148.         }
  149.         //Not using else statement as i want to check if its hit the bottom
  150.         //before performing the next incriment as if i dont, the graphics
  151.         //bug the fuck out and it looks ugly
  152.         if (plr1.hitF(bottom) == true)
  153.         {
  154.             //resets the variables
  155.             plrY[1] = 0;
  156.             plrIsFalling[1] = false;
  157.             //Starts recursion to remove embedded player
  158.             loopTillFloating(plr1);
  159.         }
  160.     }
  161.         if (plrIsFalling[2] == true)
  162.     {
  163.         if (plr2.hitF(bottom) == false)
  164.         {
  165.             plrY[2] += gravity;
  166.             plr2._y += plrY[2];
  167.         }
  168.         if (plr2.hitF(bottom) == true)
  169.         {
  170.             plrY[2] = 0;
  171.             plrIsFalling[2] = false;
  172.             loopTillFloating(plr2);
  173.         }
  174.     }
  175. };
  176.  
  177. //Removes the item from any horizontal platforms
  178. loopTillFloating = function (itemID)
  179. {
  180.     var item:MovieClip = itemID;
  181.     //moves it up one
  182.     item._y -= 1;
  183.     //if its still imbedded
  184.     if (item.hitF(bottom))
  185.     {
  186.         //do it all over again!
  187.         loopTillFloating(itemID);
  188.     }
  189. };
  190.  
  191. //Its funny how these few lines basically make everything work
  192. onEnterFrame = function ()
  193. {
  194.     //Run teh algorithms
  195.     movePlr();
  196.     falling();
  197. };
Add Comment
Please, Sign In to add comment