Advertisement
Guest User

Game Maker Smooth Platform movements v2

a guest
Dec 21st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------------------------------------------------------------------------------------
  2. //  This goes in the create event
  3. //------------------------------------------------------------------------------------
  4. move_speed = 6;  // The speed to move left and right. Change it to run faster or slower
  5. jump_speed = 12; // The height of the jump. Change this to go higher or lower
  6. grav_speed = 1;  // The gravity force. Change this to fall faster or float longer
  7. fall_speed = 0;  // The verticall speed, leave this to 0. You're not falling when the game starts.
  8.  
  9.  
  10.  
  11.  
  12.  
  13. //------------------------------------------------------------------------------------
  14. //  This goes in the step event
  15. //------------------------------------------------------------------------------------
  16. var dist, dir, is_moving;  // Define some local variables
  17. is_moving = false;     // We ain't moving... yet
  18.  
  19. // Get the keyboard inputs
  20. dir = keyboard_check( vk_right )-keyboard_check( vk_left ); // Save the horizontal direction (-1,0,1)
  21. if( dir != 0 ) image_xscale = dir;
  22.  
  23. // Move horizontaly
  24. dist = move_speed;      // The distance in pixels to move
  25.  
  26. if( dir != 0 ) // No point trying to move if the direction is 0 ( aka no keys or both keys are pressed )
  27. {
  28.         is_moving = true; // We are moving
  29.     while( !place_meeting( x+dir, y, obj_platform ) && dist > 0 )   // As long as there is no collision and we aren't done moving...
  30.         {
  31.                 x += dir;       // Move one pixel in the direction.
  32.                 dist--;         // Decrease the distance to move
  33.         }
  34. }
  35.  
  36. // Apply gravity
  37. fall_speed += grav_speed;
  38.  
  39. // Check if jumping
  40. if( keyboard_check( vk_up ) && place_meeting( x,y+1, obj_platform ) ) fall_speed = -jump_speed;
  41.  
  42. // Move vertically ( same as horizontal )
  43. dist = abs( fall_speed );
  44. dir = sign( fall_speed );
  45.  
  46. if( dir != 0 )
  47. {
  48.     while( !place_meeting( x, y+dir, obj_platform ) && dist > 0 )
  49.         {
  50.                 y += dir;
  51.                 dist--;
  52.         }
  53.     else fall_speed = 0;
  54. }
  55.  
  56. if( fall_speed < 0 )
  57. {
  58.     // You are currently jumping
  59. }
  60. else if( fall_speed > 0 )
  61. {
  62.     // You are currently falling
  63. }
  64. else if( is_moving )
  65. {
  66.     // You are walking / running
  67. }
  68. else
  69. {
  70.     // You aren't moving
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement