Advertisement
Guest User

Game Maker Moving Platform

a guest
Jul 31st, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Moving platform code
  3. ///////////////////////////////////////////////////////////////////////////////
  4.  
  5. /*
  6.     Note:
  7.     This code assumes that the platform is only travelling
  8.     horizontally or vertically, not both. (Diagonally)
  9.     Diagonal movement would also work in a very similar way
  10.     however a few lines of code would have to be tweaked in
  11.     order to handle each axis individually such as the
  12.     platform against other platform collision code.
  13.    
  14.     There's also a slight but where the player can sometimes
  15.     jump up through the platform in very specific situations
  16.     but is fixable with another check. Shouldn't matter in
  17.     most games.
  18. */
  19.  
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // Create event:
  22. ///////////////////////////////////////////////////////////////////////////////
  23.  
  24. // Give the platform a speed to move at.
  25. hSpeed = 0;
  26. vSpeed = -1;
  27.  
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // Step event:
  30. ///////////////////////////////////////////////////////////////////////////////
  31.  
  32. // Before moving check if the player is on this platform.
  33. var playerOnPlatform = place_meeting(x, y - 1, objPlayer);
  34.  
  35. // Move the platform by its horizontal and vertical speed.
  36. x += hSpeed;
  37. y += vSpeed;
  38.  
  39. // Check if the platform has hit another platform of any type.
  40. if(place_meeting(x, y, objPlatform))
  41. {
  42.     // If so then move back away from the platform that has
  43.     // been hit.
  44.     x -= hSpeed;
  45.     y -= vSpeed;
  46.    
  47.     // And reverse the direction that the platform is
  48.     // travelling in.
  49.     hSpeed *= -1;
  50.     vSpeed *= -1;
  51. }
  52. // If the platform has successfully moved without a collision
  53. // and the player is on top then move the player with it.
  54. else if(playerOnPlatform || place_meeting(x, y, objPlayer))
  55. {
  56.     // Tell the player to execute the code inside the curly
  57.     // brace brackets.
  58.     with(objPlayer)
  59.     {
  60.         // Move at the same speed that the platform is moving
  61.         // at.
  62.         x += other.hSpeed;
  63.         y += other.vSpeed;
  64.        
  65.         // Check for collisions with other platforms and if any
  66.         // occur move back to original position.
  67.         if(place_meeting(x, y, objPlatform))
  68.             x -= other.hSpeed;
  69.            
  70.         if(place_meeting(x, y, objPlatform))
  71.             y -= other.vSpeed;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement