Code the Obstacle
With the obstacle created, code is now required to make the player or game react to hitting the character hitting the obstacle.
For this example, we will store the characters start position and reset that when the character hits the obstacle.
Example Code
On the create event of the Object, the following code stores the X and Y position of the object when the object is created – when the level first loads.
These values will be used when the object hits an obstacle.
//Sets the starting x position to the current x position start_x_position = x; //Sets the starting y position to the current y position start_y_position = y;
On the collision event when the player collides with the obstacle, the following code sets the starting X and Y position back to the original values that were saved in the create event.
This resets the object to its original position.
//Sets the x position back to its original starting position x = start_x_position; //Sets the y position back to its original starting position y = start_y_position;