Hello, and welcome to the section. In this section we'll be going through the theory of drag and drop programming before we start making our game. So in this lecture, we learn how actions can be used to move instances. each instance has two values for its position in the role, an x value and a y value. The X is the horizontal position, and the y is the vertical position. So together, they tell where the instance is in the room.
So by changing the x and y values, you can make an instance move in any direction in gamemaker, increasing the x moves on instance to the right, but increasingly it moves it to the left. Similarly, increasing the Y moves it downwards, but decreasing it moves it upwards. So by simply adding and subtracting from the x and y, you can make an instance move. So now let's open up gamemaker. I've imported a new sprite here for our object. So I'll go to the resource bar and open the object that we created.
In the previous lecture, I'll click here and assign a new sprite to it. Now using events and actions will make the shape move using the arrow keys. So I'll go to add event go under key down and select left the key down events from as long as the key is held down. So as long as you have the left arrow key held down, this event will keep running. Now we'll add an action in this event, you can see all of the actions here, and you can search for one here. so here also Let's jump, I need this jump to point action, so I'll drag it into the events.
Now this action is used to change the x and y coordinates of the instance. But if you take relative, the values you enter here will be added to the x and y. So this way, you can make the instance move. As I said earlier, you need to decrease the x value to make it move left. So to do that, I'll take the relative box for the eggs and in the eggs, I'll enter minus five. So as long As the left keys, press five will be subtracted from the stances x.
And here you can see an overview of the action. Now we need to test it. So I'll open the default room that we have here. I'll drag the object inside the room to place an instance of it. And now I'll click here to run the game. As you can see, the instance moves to the left if I press the left arrow key, now I'll close the game and the rule and go back to the object.
Now I'll right click on the event, select Duplicate, then go on to key down and select the right event. This is the event for the right arrow key. Since we duplicated the left event to create this one, it has the same action inside it. Now I'll go to the action and change the relative x value from minus five to five. So when you hold the right arrow key five will be added to the x and the instance will move to the right now we'll go to add event. Under key down, select the down event.
Now this is the event for the down arrow key. So I go to the actions window and add the jump to point action. I like the y as well to identify here. So using the down arrow key five will be added to the y and our instance will move downwards. Now I'll duplicate the down event as well as like the key down up event. This event is for the up arrow key.
So to make it move up, I'll change the relative y value from five to minus five. Now all four arrow key events have been Sara swirl run the game. Now you can see that I'm able to move in all directions. Now you can see that I'm using the value five in the actions. So when you press an arrow key, the instance moves five pixels in that direction. So you can increase or decrease this value to make it move faster or slower.
But then you would have Make the change in all these events. So in the next lecture we will learn how a woman speed can be changed without having to modify all the events.