How to make a platformer in game maker studio 2
If the conditions are not true, which means the player is not jumping on top of the enemy, the player loses and the room is restarted. When both conditions are true, the player destroys the enemy object and makes a jump. The second condition checks if the player’s vertical speed is more than 0, which implies that it is falling down. The first condition checks if the player is above the enemy. The two conditions check if the player is jumping on top of the enemy. Other is used to reference the other object in the collision, which here is obj_player. Open your obj_enemy and add a Collision with obj_player event. Let’s make it so that if the player touches the enemy, the level (room) gets restarted, implying that the player lost, but if the player jumps on top of the enemy, the enemy object gets destroyed, implying that the enemy died. However, the player cannot yet interact with it. You can place the enemies in the room and see them moving around. Without this condition, the enemy will start walking in midair, so with it the enemy changes direction when it comes to the floor’s edge. The first condition checks for empty space in front of the object and the second one checks for an empty space at the floor before it. The two conditions in the second line check for the floor edge. It changes true to false and vice-versa in the condition. The exclamation mark (!) before place_free() is the same as the keyword not we used in the previous article. The one condition in the first line checks for a solid object before the enemy, so it can turn away and not go through it. So what this does is change the state so that the enemy changes its direction. If not, which implies that the state is “right”, it will change it to “left”. It checks if the state if “left”, then changes it to “right”. Let me first explain the highlighted code. If (place_free(x + hspeed, y) and place_free(x + hspeed * 16, y + 16)) if (state=”left”) state = “right” else state = “left” If (!place_free(x + hspeed, y)) if (state=”left”) state = “right” else state = “left”
#How to make a platformer in game maker studio 2 code#
We will add some more code to the Step event. If not, and if the state is “right” (which it will obviously be, what other option does state have?) then the enemy will move right (because of hspeed = 2). The states can be either left or right, so according to this condition, if the state is “left”, the hspeed will be -2 and the enemy will move to the left. So, state will be chosen randomly from either “left” or “right” at the object’s initiation. So add the Create event and put this code into it:Ĭhoose() selects a random item out of those specified inside its brackets. If (price=350) make_something_happen(now) Now the variable price holds the value 350. Our obj_enemy can move left and right, so inside it we will create a variable called state which can either be “left” or “right” and defines where the object will move.Ī variable stores some value and can be checked in a condition. Name it spr_enemy.Ĭreate an object for it ( obj_enemy). Enemy ObjectĬreate a 32×32 sprite for our enemy, the same way we did for our player. This article is mainly aimed at complete beginners. In this one we’re going to make enemies for our platformer game, which move around and can be defeated by the player by jumping on it. This mimics reality because when you slow down you get the benefit of friction but when accelerating it works against you.Hello again! In the previous article we created a basic platformer using GML (Game Maker Language). It also can't be larger than your current speed and is stronger if you aren't moving. I added a friction vector that constantly goes against your velocity. Check the magnitude of the the velocity to ensure its not greater than the top speed. Then to add it to your current velocity (both x speed and y speed). The trick is to make another acceleration vector (or two variables) with the magnitude of acceleration rate and the direction that you want to go.