Enemy animation

sweep

Member
Hi
( I know this topic must have been brought up a thousand times before.)
I am working on a top down game. I've been through the forum looking for a way to get the enemy to animate when chasing the player. I have the enemy sprites set up for 4 directional movement. I've tried tweaking the player animation code but i can't figure out how to tell the enemy its xx, yy ??

player animation code

xx = move_right - move_left;
yy = move_down - move_up;

hspd = xx*spd;
vspd = yy*spd;

if (keyboard_check(vk_right))
{
xx += hspd;
image_speed = hspd/3
sprite_index = spr_player_right;
}

if (keyboard_check(vk_up))
{
yy -= vspd;
image_speed = vspd/3
sprite_index = spr_player__up;
}
if (keyboard_check(vk_left))
{
xx -= hspd;
image_speed = hspd/3
sprite_index = spr_player_left;
}
if (keyboard_check(vk_down))
{
yy+= vspd;
image_speed = vspd/3
sprite_index = spr_player;
}

if (vspd == 0) and (hspd == 0)
{
image_speed = 0

}
 

Bentley

Member
It looks a bit odd. You just need something simple that gets your x input and your y input and moves you:
Pseudo code (using your variable names)
Code:
var xx, yy;
xx = keyboard_check(vk_right) - keyboard_check(vk_left); //1, 0, or -1
yy = keyboard_check(vk_down) - keyboard_check(vk_up);

if (xx != 0 || yy != 0)
{
    hspd = xx * spd;
    vspd = yy * spd
    // Set animation here
}
else
{
    // Turn off animation here
}
I'm making the assumption that hspd, vspd, and spd are all instance variables. Keep in mind you also need to move your "x" and "y" position, both built in variables, based on hspd and vspd. You will need to collision check as well. Hope I helped.
 

sweep

Member
thanks you
I used your code and the enemy animate but they move when i use the keyboard ? they don't stop moving. I've gone wrong.

edit Code

var xx, yy;
xx = keyboard_check(vk_right) - keyboard_check(vk_left); //1, 0, or -1
yy = keyboard_check(vk_down) - keyboard_check(vk_up);

if (xx != 0 || yy != 0)
{
hspd = xx * spd;
vspd = yy * spd
// if (keyboard_check(vk_right))
{
xx += hspd;
image_speed = hspd/3
sprite_index = spr_enemy_right;
}

if (keyboard_check(vk_up))
{
yy -= vspd;
image_speed = vspd/3
sprite_index = spr_enemy_up;
}
if (keyboard_check(vk_left))
{
xx -= hspd;
image_speed = hspd/3
sprite_index = spr_enemy_left;
}
if (keyboard_check(vk_down))
{
yy+= vspd;
image_speed = vspd/3
sprite_index = spr_enemy;
}


else
image_speed = 0
{

}
 
Last edited:

TheouAegis

Member
Yeah because he didn't understand your question. As you deduced, really all you need to do is change xx and YY for the enemy, for the most part. so first off, get rid of any references to the keyboard. What you actually use for xx and YY depends on what you want the enemy to do. If you want the enemy to move toward the player, then you can set

xx = sign(player.x - x);
yy = sign(player.y - y);
 

Bentley

Member
thanks you
I used your code and the enemy animate but they move when i use the keyboard ? they don't stop moving. I've gone wrong.

edit Code

var xx, yy;
xx = keyboard_check(vk_right) - keyboard_check(vk_left); //1, 0, or -1
yy = keyboard_check(vk_down) - keyboard_check(vk_up);

if (xx != 0 || yy != 0)
{
hspd = xx * spd;
vspd = yy * spd
// if (keyboard_check(vk_right))
{
xx += hspd;
image_speed = hspd/3
sprite_index = spr_enemy_right;
}

if (keyboard_check(vk_up))
{
yy -= vspd;
image_speed = vspd/3
sprite_index = spr_enemy_up;
}
if (keyboard_check(vk_left))
{
xx -= hspd;
image_speed = hspd/3
sprite_index = spr_enemy_left;
}
if (keyboard_check(vk_down))
{
yy+= vspd;
image_speed = vspd/3
sprite_index = spr_enemy;
}


else
image_speed = 0
{

}
You're very close. You don't need the keyboard_check inputs below (vk_up vk_left, vk_down, vk_right) b/c you already got the input and stored it in xx and yy.
My guess as to why you can't stop moving is that your not setting hspd to 0 and vspd when there's no input in that direction.
 

sweep

Member
@Bentley
I removed the keyboard checks (code below ) The the screen lit up with errors ?
var xx, yy;
xx = keyboard_check(vk_right) - keyboard_check(vk_left); //1, 0, or -1
yy = keyboard_check(vk_down) - keyboard_check(vk_up);
 

sweep

Member
EDIT : I also have this code for following the player
if (point_distance(x, y, obj_player.x, obj_player.y) < 80)
{
move_towards_point(obj_player.x, obj_player.y, 1);

}
@TheouAegis should your code look like this ?

xx = sign(obj_player.x - x);
yy = sign(obj_player.y - y);

xx += hspd;
image_speed = hspd/3
sprite_index = spr_enemy_right;

yy -= vspd;
image_speed = vspd/3
sprite_index = spr_enemy_up;

xx -= hspd;
image_speed = hspd/3
sprite_index = spr_enemy_left;

yy+= vspd;
image_speed = vspd/3
sprite_index = spr_enemy;
 
Top