External Exam Download Resources Web Applications Games Recycle Bin

move ball | gml

 TypeDesc
keyboard events with GML
keyboard_check(key)functioncheck if key is down
keyboard_check_pressed(key)functioncheck if key is pressed
keyboard_check_released(key)functioncheck if key is released
reading keys with GML
vk_upconstant'virtual keyboard' key for up
vk_down, vk_space etcconstantsas above
ord("A")functionreturns the ascii code for the letter "A"
using keyboard variables with GML
keyboard_keyvariablestores current key being used
keyboard_lastkey, keyboard_lastchar, keyboard_stringvariableslast key, last character and last 1024 characters (respectively)
using mouse with GML
mouse_buttonvariablestores current mouse button being pressed
mouse_x, mouse_yvariablescurrent axis position of mouse within room
mouse_check_button(button)functioncheck for a button constant
mb_left, mb_right, mb_middle, mb_none, mb_anyconstantsuse in the above function (mb_ = mouse button)
SpriteImages
sprBall
ObjectSpriteProperties
objBallsprBallVisible

objBall Step

if (keyboard_check(vk_left)){
  x = x - 3;	
}

if (keyboard_check(vk_right)){
  x = x + 3;	
}

if (keyboard_check(vk_up)){
  y = y - 3;	
}

if (keyboard_check(vk_down)){
  y = y + 3;	
}
Challenge Activities
Replace the code in the objBall Step event with the following:

1. Interchange functions and keys - how do these changes affect the behaviour of the controls?

objBall Step

if (keyboard_check(ord("A"))){
  x = x - 3;	
}

if (keyboard_check_pressed(vk_right)){
  x = x + 3;	
}

if (keyboard_check_released(vk_up)){
  y = y - 3;	
}

if (keyboard_check(vk_down)){
  y = y + 3;	
}
2. Working with the keyboard variables using a switch case expression - try this and decide which method you prefer.

objBall Step

switch (keyboard_key) {
   case vk_numpad2: y = y + 3; break;
   case vk_numpad4: x = x - 3; break;
   case vk_numpad6: x = x + 3; break;
   case vk_numpad8: y = y - 3; break;
}
3. Try mouse control - can you get this to work without the mouse click (i.e. follow the cursor in the room?)

objBall Step

if (mouse_check_button(mb_any)) {
	direction = point_direction(x,y,mouse_x,mouse_y);
	speed = 3;
}