Those are just values I keep around “just in case”.Hello again! Create a Platformer Game with GML Sometimes I might need to store those values elsewhere along the execution of the movement code. Sometimes I need to know their positions before moving them at all (hence their values get refreshed before the repeat-loops). I might use the xprev and yprev as I see fit for my particular objects. That’s all for now… About the xprev and yprev. Round the speeds while keeping track of the sub-pixel values, move 1px in the direction of the speed for as many times as the value of the speed until we collide with something. Every moving object may execute a custom variation of the previous scripts. That’s all folksĪnyway this is the basic of my platformer engine. The basic concept for slopes is there but beware that for moving platforms I’m using a totally different solution.
You can check Zack’s code in the meantime. I will be talking about slopes and moving platforms in the next article. You’ll notice this is called only if the object is actually moving down in the scr_move script.
Var on_moving_platform = collision_line(bbox_left, bbox_bottom + 1, bbox_right, bbox_bottom + 1, obj_platform_moving, false, true) Var on_one_way_platforms = collision_line(bbox_left, bbox_bottom + 1, bbox_right, bbox_bottom + 1, obj_one_way_platform, false, true) Var on_crate = collision_line(bbox_left, bbox_bottom + 1, bbox_right, bbox_bottom + 1, obj_crate_solid, false, true) Var on_ground = collision_line(bbox_left, bbox_bottom + 1, bbox_right, bbox_bottom + 1, obj_solid, false, true) If collision_rectangle(bbox_left + xdir, bbox_bottom, bbox_right + xdir, bbox_top, obj_solid, false, true) ||Ĭollision_rectangle(bbox_left + xdir, bbox_bottom, bbox_right + xdir, bbox_top, obj_crate_solid, false, true)Īnd the is_on_ground script looks like this one ///is_on_ground() Var coll = collision_line(bbox_left, bbox_top - 1, bbox_right, bbox_top - 1, obj_solid, false, true) My code looks more like this one, indeed… ///scr_move() Instead I use multiple line_collision with different objects based on the direction I’m actually headed. I’m not using a single place_meeting anymore. What I use now is quite customized in the collision check part. This is the actual movement code that must be placed in the step event after you have modified the xVel and yVel as you see fit for you objects (like player input, jumps, falling mechanics and so on) ///scr_move()Īs you can see this looks terribly similar to Zack Bell’s code. That’s why I define my own xprev and yprev as well. We might need to use our own variables for the previous X and Y positions as well. Previous X/Y coords to use as we see fit Initialize the variables used for movement code This script is called in the create event of moving objects
This is a stripped down version of what I’m using since I’ve customized mine heavily (with sprite management stuff we don’t need for movement) ///scr_platformer_init() in my game that would be the Player, the broken pieces of pots and crate boxes, bombs, collectible items…) must initialize some variables that will be used in lieu of GM internal ones.
The following script is executed once, in the create event by those objects that need to move. I prefer to directly manipulate the X and Y coordinates of my objects, so I usually implement my own speed variables.
hspeed, vspeed, speed, direction, gravity and so on) there’s only so much control you can have over those variables and how they interact with your objects… sooner or later you’ll find yourself with no idea of what’s going on in your game. Enjoy! Don’t use GameMaker’s internal variables.įor speeds (vertical and horizontal) I never use the internal variables (e.g. It’s 2.8x more efficient than the code below. I’ve rewritten it and make some speed tests. UPDATE: Please take a look at this other, optimized code. Once you grasp the basics, you can edit the code to adapt to your specific needs. The following code is adapted from Zack Bell’s platformer code (from his article Understanding Collisions pt.2). Here’s what I actually use for my engine. It’s only natural that I spent the past year studying and refining platformer engines for GameMaker: Studio.