• No results found

GML: User interaction

In document Designing Games with Game Maker (Page 107-111)

There is no game without interaction with the user. The standard way of doing this in

Game Maker is to put actions in mouse or keyboard events. But sometimes you need more control. From within a piece of code you can check whether certain keys on the keyboard are pressed and you can check for the position of the mouse and whether its buttons are pressed. Normally you check these aspects in the step event of some controller object and take action accordingly. The following variables and functions exist:

mouse_x* X-coordinate of the mouse. Cannot be changed.

mouse_y* Y-coordinate of the mouse. Cannot be changed.

mouse_button Currently pressed mouse button. As value use mb_none, mb_any, mb_left, mb_middle, or mb_right.

keyboard_lastkey Keycode of last key pressed. See below for keycode constants. You can change it, e.g. set it to 0 if you handled it.

keyboard_key Keycode of current key pressed (see below; 0 if none).

keyboard_lastchar Last character pressed (as string).

keyboard_string String containing the last at most 80 characters typed. This string will only contain the printable characters typed. It also correctly responds to pressing the backspace key by erasing the last character.

Sometime it is useful to map one key to another. For example you might want to allow the player to use both the arrow keys and the numpad keys. Rather than duplicating the actions you can map the numpad keys to the arrow keys. Also you might want to implement a mechanism in which the player can set the keys to use. For this the following functions are available

keyboard_set_map(key1,key2)Maps the key with keycode key1 to key2.

keyboard_get_map(key)Returns the current mapping for key.

keyboard_unset_map()Resets all keys to map to themselves.

To check whether a particular key or mouse button is pressed you can use the following functions. This is in particular useful when multiple keys are pressed simultaneously.

keyboard_check(key) Returns whether the key with the particular keycode is pressed.

keyboard_check_direct(key) Returns whether the key with the particular keycode is pressed by checking the hardware directly. The result is independent of which application has focus. It allows for a few more checks. In particular you can use keycodes vk_lshift, vk_lcontrol, vk_lalt, vk_rshift, vk_rcontrol and vk_ralt to check whether the left or right shift, control or alt key is pressed. (This does not work under windows 95!).

mouse_check_button(numb) Returns whether the mouse button is pressed (use as values mb_none, mb_left, mb_middle, or mb_right).

The following routines can be used to manipulate the keyboard state:

keyboard_get_numlock() Returns whether the numlock is set.

keyboard_set_numlock(on) Sets (true) or unsets (false) the numlock. (Does not work under Windows 95.)

keyboard_key_press(key) Simulates a press of the key with the indicated keycode.

keyboard_key_release(key) Simulates a release of the key with the indicated keycode.

The following constants for virtual keycodes exist:

vk_nokey keycode representing that no key is pressed

vk_anykey keycode representing that any key is pressed

vk_left keycode for left arrow key

vk_right keycode for right arrow key

vk_up keycode for up arrow key

vk_down keycode for down arrow key

vk_enter enter key

vk_escape escape key

vk_space space key

vk_shift shift key

vk_control control key

vk_altalt key

vk_backspace backspace key

vk_tab tab key

vk_home home key

vk_end end key

vk_deletedelete key

vk_insert insert key

vk_pageup pageup key

vk_pagedown pagedown key

vk_pause pause/break key

vk_printscreen printscreen/sysrq key

vk_f1vk_f12 keycodes for the function keys F1 to F12

vk_numpad0vk_numpad9 number keys on the numeric keypad

vk_multiply multiply key on the numeric keypad

vk_divide divide key on the numeric keypad

vk_add add key on the numeric keypad

vk_subtract subtract key on the numeric keypad

vk_decimal decimal dot keys on the numeric keypad

For the letter keys use for example ord('A'). (The capital letters.) The following constants can only be used in keyboard_check_direct:

vk_lshift left shift key

vk_lcontrol left control key

vk_lalt left alt key

vk_rshift right shift key

vk_rcontrol right control key

vk_ralt right alt key

They do not work on older version of Windows!

For example, assume you have an object that the user can control with the arrow keys you can put the following piece of code in the step event of the object:

{ if (keyboard_check(vk_left)) x -= 4; if (keyboard_check(vk_right)) x += 4; if (keyboard_check(vk_up)) y -= 4; if (keyboard_check(vk_down)) y += 4; }

Of course it is a lot easier to simply put this in the keyboard events. There are three additional functions related to interaction.

keyboard_clear(key) Clears the state of the key. This means that it will no longer generate keyboard events until it starts repeating.

mouse_clear(button) Clears the state of the mouse button. This means that it will no longer generate mouse events until the player releases it and presses it again.

io_clear() Clears all keyboard and mouse states.

io_handle() Handle user io, updating keyboard and mouse status.

keyboard_wait() Waits till the user presses a key on the keyboard.

31.1 Joystick support

There are some events associated with joysticks. But to have full control over the joysticks there is a whole set of functions to deal with joysticks. Game Maker supports up to two joysticks. So all of these functions take a joystick id as argument.

joystick_exists(id) Returns whether joystick id (1 or 2) exists.

joystick_name(id) Returns the name of the joystick

joystick_axes(id) Returns the number of axes of the joystick.

joystick_buttons(id) Returns the number of buttons of the joystick.

joystick_has_pov(id) Returns whether the joystick has point-of- view capabilities.

joystick_direction(id) Returns the keycode (vk_numpad1 to vk_numpad9) corresponding to the direction of joystick id (1 or 2).

joystick_check_button(id,numb) Returns whether the joystick button is pressed (numb in the range 1-32).

joystick_xpos(id) Returns the position (-1 to 1) of the x-axis of joystick id.

joystick_ypos(id)Returns the joysticks y-position.

joystick_zpos(id)Returns the joysticks z-position (if it has a z-axis).

joystick_rpos(id)Returns the joysticks rudder position (or fourth axis).

joystick_upos(id)Returns the joysticks u-position (or fifth axis).

joystick_vpos(id)Returns the joysticks v-position (or sixth axis).

joystick_pov(id) Returns the joysticks point-of view position. This is an angle between 0 and 360 degrees. 0 is forwards, 90 to the right, 180 backwards and 270 to the left. When no point-of-view direction is pressed by the user -1 is returned.

In document Designing Games with Game Maker (Page 107-111)