Game Development Reference
In-Depth Information
if (keyboard_check(key_left)){
x-=max_spd;
}
if (keyboard_check(key_right)){
x+=max_spd;
}
We can now move left and right, but what about up and down? We need to either re-write
or copy the preceding code onto a new line below what we already have. Then, for the
first if statement, we need to change key_left to key_up and x-=max_spd to y-
=max_spd .
Now, for the second if statement, change key_right to key_down and
x+=max_spd to y+=max_spd .
Our code should now look like this:
if (keyboard_check(key_left)){
x-=max_spd;
}
if (keyboard_check(key_right)){
x+=max_spd;
}
if (keyboard_check(key_up)){
y-=max_spd;
}
if (keyboard_check(key_down)){
y+=max_spd;
}
As you can see in the preceding code, we modified the third and fourth statement to
change the y value instead of the x value. This is because left and right is done on the ho-
rizontal x axis and up and down is done on the vertical y axis.
We are now checking for every key and moving accordingly. If we put this into our room
and run the game, we should be able to move the object around using the arrow keys.
Notice that if you are holding both the left and right or up and down arrow keys at the
same time, the object will not move. This is because by holding down the left key, we
Search WWH ::




Custom Search