• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [Solved] Enemy sprites won't animate right

C

Crimson Flames

Guest
I set something up for enemy animations, but it isn't working quite right. The up and down animations work fine (usually), but the left and right animations don't work at all. This code is run in the step event for the enemy, after the movement code. I've tried using this code in the draw event, but I have the same problem.
Code:
//Animation
Right = (sign(obj_Player.x - x) = 1);
Left = (sign(obj_Player.x - x) = -1);
Up = (sign(obj_Player.y - y) = -1);
Down = (sign(obj_Player.y - y) = 1);

if(Right){
sprite_index = spr_GlitchRight;
image_speed = AnimationSpeed;
}

if(Left){
sprite_index = spr_GlitchLeft;
image_speed = AnimationSpeed;
}

if(Down){
sprite_index = spr_GlitchDown;
image_speed =AnimationSpeed;
}

if(Up){
sprite_index = spr_GlitchUp;
image_speed = AnimationSpeed;
}
if (!Down and !Right and !Left and !Up){
image_index = 0;
image_speed=0;
}
 
Last edited by a moderator:

NicoDT

Member
There are several problems with your code.

Code:
//Animation
Right = (sign(obj_Player.x - x) = 1);
Left = (sign(obj_Player.x - x) = -1);
Up = (sign(obj_Player.y - y) = -1);
Down = (sign(obj_Player.y - y) = 1);
You're using the "=" twice in each line, what you should do is the following
Code:
//Animation
Right = sign(obj_Player.x - x);
Left = sign(obj_Player.x - x) ;
Up = sign(obj_Player.y - y) ;
Down = sign(obj_Player.y - y);
This will already assign the value 1 or -1 to the variables.

Now with this part:
Code:
if(Right){
sprite_index = spr_GlitchRight;
image_speed = AnimationSpeed;
}

if(Left){
sprite_index = spr_GlitchLeft;
image_speed = AnimationSpeed;
}

if(Down){
sprite_index = spr_GlitchDown;
image_speed =AnimationSpeed;
}

if(Up){
sprite_index = spr_GlitchUp;
image_speed = AnimationSpeed;
}
It this enemy is placed diagonally from the player, then you would have two of the states (left or right) and (up or down), meaning that it would enter two of those brackets (That's why your horizontal sprites don't work).

The solution for your problem would be something like this:
Code:
angle = point_direction(x, y,obj_Player.x,obj_Player.y);
direc = "right";
if (angle >= 45 and angle <135) direc = "up";
if (angle >= 135 and angle <215) direc = "left";
if (angle >= 215and angle <315) direc = "down";

switch(direc) {
   case "right":  sprite_index = spr_GlitchRight; image_speed = AnimationSpeed; break;
   case "left":    .......  ; break;
   case "down":    .......  ; break;
   case "up":    .......  ; break;
   default: break;
}


;
 
C

Crimson Flames

Guest
OK, thanks. I was using some of the stuff I learned in @uheartbeast 's tutorial, and I tried to change it a bit so I could do custom movement sprites. Clearly that didn't work out too well. :|
Luckily, your solution works. Thanks so much, and also thank you for being nice and actually explaining what went wrong.
 
Top