controlling animation | gml
| Type | Desc | |
|---|---|---|
| working with images in GML | ||
| sprite_index | variable | current sprite (not the current sub-image within the sprite) |
| image_index | variable | the current sub-image showing within the sprite |
| image_speed | variable | the speed that the sub-images change within the sprite |
| distance to point | ||
| distance_to_point(x,y) | function | returns the distance in pixels from the calling object to a point |
| Sprite | Images |
|---|---|
| sprDown | |
| sprUp | |
| sprLeft | |
| sprRight | |
In the sprite editor, use Import Strip Image:![]() Repeat for each sprite (down, up, left & right). ![]() Use Vertical Cell Offset to select different rows:
|
|
| Object | Sprite | Properties |
|---|---|---|
| objHuman | sprRight | Visible |
objHuman Step
if keyboard_check(vk_anykey){
image_speed = 0.3; //moving character, direction:
switch (keyboard_key) {
case vk_up:
sprite_index = sprUp;
y = y - 3;
break;
case vk_down:
sprite_index = sprDown;
y = y + 3;
break;
case vk_left:
sprite_index = sprLeft;
x = x - 3;
break;
case vk_right:
sprite_index = sprRight;
x = x + 3;
break;
}
} else {
image_speed = 0.0; //still character
}
1. Try this alternative mouse follow code:
objHuman Step
direction = point_direction(x,y,mouse_x,mouse_y);
if (distance_to_point(mouse_x, mouse_y) < 1.0) {
speed = 0;
image_speed = 0.0;
} else {
speed = 3;
image_speed = 0.3;
if (direction > 45 and direction < 135) { sprite_index = sprUp; }
if (direction >= 135 and direction <= 225) { sprite_index = sprLeft; }
if (direction > 225 and direction < 305) { sprite_index = sprDown; }
if (direction >= 305 or direction <= 45) { sprite_index = sprRight; }
}
2. Can you make the player follow faster if i hold a mouse button down?


