• 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!

Sprite not animating.

M

MIWICP86

Guest
I've been working on and off following a tutorial to make a game, all going really well until I had a break. Came back to GM and things aren't working as they were before. I have a ranged enemy unit that will walk and shoot a projectile, but not swap to the enemy attack animation? I'm sure I've coded it properly, gone back to check the tutorial and have gone through the code stated there, but to no avail. Any help is greatly appreciated!

ENEMY ATTACK STATE


The code highlighted has worked fine for other enemy attack states? Thank you in advance and apologies for what is probably a very easy thing to correct.

Cheers
 
M

MIWICP86

Guest
Hey all, haven't had a response, so I'm including some further information. Again, apologies if this is a really noob question, but I'm stumped as to why the sprite is not switching to it's attack animation. Any help is greatly appreciated.

CREATE SCRIPT
Code:
event_inherited();
initialize_movement_entity(0.25, .5, obj_solid);
max_health_ = 3;
health_ = max_health_;

enum lobber {
    hit,
    idle,
    move,
    attack,
    wait
}


starting_state_ =lobber.idle;
state_ = starting_state_;


image_index = 0;
image_speed = 1;
image_xscale = choose(-1, 1);

alarm[1] = global.one_second * random_range(0, 1);
alarm[2] = global.one_second * random_range(6, 8);
range_ = 140;
USER EVENT 1 (IDLE)
Code:
/// @description idle state
image_speed = 1;
image_index = 0;




if alarm[1] <= 0 {
    alarm[1] = random_range(2, 4) * global.one_second;
    state_ = lobber.move;
    direction_ = random(360);
}

lobber_attack()
USER EVENT 2 (MOVE STATE)
Code:
/// @description move state
image_speed = 1;
sprite_index = spr_lobber_run_right;
set_sprite_facing();

if alarm[1] <= 0 {
    apply_friction_to_movement_entity();
} else {
    add_movement_maxspeed(direction_, .5, .75);   
}

move_movement_entity(true);

if speed_ == 0 {
    alarm[1] = random_range(1, 3) * global.one_second;
    state_ = lobber.idle;
}


if alarm[2] <= 0 and distance_to_object(obj_idle_up) < range_ {
    state_ = lobber.attack;   
}
USER EVENT 3 (ATTACK STATE)
Code:
/// @description attack state
image_speed = 1;
sprite_index = spr_lobber_attack_right;
set_sprite_facing()

if not instance_exists(obj_idle_up) {
    state_ = lobber.move;
    exit;
}

apply_friction_to_movement_entity();
move_movement_entity(true);

if speed_ == 0 {
    alarm[2] = global.one_second * random_range(2, 4);
    var _direction = point_direction(x, y, obj_idle_up.x, obj_idle_up.y) + random_range(-25, 25);
    var _stinger = instance_create_layer(x, y, "Instances", obj_lobber_skullshot);
    _stinger.direction = _direction;
    _stinger.image_angle = _direction;
    _stinger.speed = 1.75;
    state_ = lobber.move
}
Thanks again for any help.
 

Slyddar

Member
Try running in debugger mode (F6). Place a breakpoint (F9) in the attack state, and then step through the code (F11) to see when the sprite changes to something else.
 
M

MIWICP86

Guest
Hey, thank you for the reply!
So I tried what you have said to do, but am not too sure of what to look out for/the next steps.
Soon as I go through the game and the enemy attacks, the game pauses and it goes to the debug (which I'm taking is correct), straight to the line I've highlighted as a break point (see attached photo).



I step through the code and it brings me to a script called set_sprite_facing (see below script).

Code:
var _x_speed = lengthdir_x(speed_, direction_);
if _x_speed != 0 {
    image_xscale = sign(_x_speed);  
}
I also tried it in the 2nd User Event, when the alarm is triggered and the sprite is supposed to change from walking to attacking (see attached photo).



this brings me to the enemy object step event, particularly the section highlighted.

Code:
depth =- y;
if health_ <= 0 && state_ != enemy.attack {
    instance_destroy ();
}

if state_ != noone {
    event_user (state_);   
}
So now I'm stumped as what to do next? Do I need to change any code here? Apologies for my lack of skill and understanding, and thank you so much for your help!
 

curato

Member
Double check all the code for image_speed = 0; if it is there make sure that the logic is such that it is impossible for that code to run while you are playing the attack animation. Also, did you override the draw event? If so, seeing that may help.
 
M

MIWICP86

Guest
Double check all the code for image_speed = 0; if it is there make sure that the logic is such that it is impossible for that code to run while you are playing the attack animation. Also, did you override the draw event? If so, seeing that may help.
There's no code for image_speed = 0 for this enemy type or the enemy_object parent. As for the draw event that just creates the shadow below the enemy...

Code:
draw_sprite(spr_medium_shadow, 0, x, y+12);
draw_self();
I'm really, really stumped? Do I just delete the object and then redo it?

Also, thank you for your reply!
 

Kyon

Member
Although I'm not sure what user events really do, I noticed two things that maybe help.
In the idle user event you set the image_index to 0. So it's always the first frame, not animating.
Code:
/// @description idle state
image_speed = 1;
image_index = 0;
Then, this probably is just a static shadow sprite, but you did draw the shadow and only it's first frame:
Code:
draw_sprite(spr_medium_shadow, 0, x, y+12);
As the "0" is again the image_index. And if it's an animated sprite should be set to "image_index".

But idk, I don't think these are problems really.
Where do the user_events get triggered?

(so like, can we see the step event, and maybe that "set_sprite_facing" script.)
 
M

MIWICP86

Guest
Although I'm not sure what user events really do, I noticed two things that maybe help.
In the idle user event you set the image_index to 0. So it's always the first frame, not animating.
Code:
/// @description idle state
image_speed = 1;
image_index = 0;
Then, this probably is just a static shadow sprite, but you did draw the shadow and only it's first frame:
Code:
draw_sprite(spr_medium_shadow, 0, x, y+12);
As the "0" is again the image_index. And if it's an animated sprite should be set to "image_index".

But idk, I don't think these are problems really.
Where do the user_events get triggered?

(so like, can we see the step event, and maybe that "set_sprite_facing" script.)
Hey, thank you for the reply! I changed the image_index for the idle state but it didn't activate the attack animation. As for the shadow there is only one frame for the shadow, so that's why its set at 0.

Here's the step event

Code:
depth =- y;
if health_ <= 0 && state_ != enemy.attack {
    instance_destroy ();
}

if state_ != noone {
    event_user (state_);   
}
and the set_sprite_facing script

Code:
var _x_speed = lengthdir_x(speed_, direction_);
if _x_speed != 0 {
    image_xscale = sign(_x_speed);   
}
Cheers!
 
Top