A sprite is a 2D image or animation that represents an object in a video game. Think of it like a character, item, or background element that you can see and sometimes control in a game.
You cannot control a sprite directly. To add it to a game we need to create an object.
We can then program the object to do want we want. In our case we will want to have the player object move when we press the arrow keys.
Events
To make the object move we need to trigger some code that will change the objects co-ordinates in the room.
An event is an action or occurrence that triggers a set of instructions. For example pressing a key on the keyboard.
To move the object we need to change the x and y co-ordinates that are stored in built in variables x and y.
To move up we need to reduce the y value
with the code: y = y - 3;
To move down we need to increase the y value
with the code: y = y + 3;
To move left we need to reduce the x value with the code: x = x - 3;
To move right we need to increase the x value with the code: x = x + 3;
We can add events to our object, to trigger the code.
Each object has ready made speed and direction variables.
To move the player we can change the direction and set a speed each time a key is pressed.
To move up we need to point upwards
with the code: direction = 90;
To move down we need to point downwards
with the code: direction = -90;
To move left we need to point to the left
with the code: direction = 180;
To move right we need to point to the right
with the code: direction = 0;
We can add events to our object, to trigger the code.
If you test the game the player object will now move around the screen, however it can go off the edge of the screen, and may get lost.
We can get the player to automatically re-appear on the other side of the screen my using the move_wrap() function.
The game will need to continuously check to see if it has gone off the screen. To check continuously we use the Step event.
Add a step event and add the following code: move_wrap(true, true, 0)