Sprite Won't Show All Frames

T2008

Member
So, I've had a lot of trouble with the animation end event. Right now, I've tested my jump code and the animation end event never triggers. Through debug, it shows that the last two frames of the sprite are never shown and I have no idea why. I can only get the animation to stop at image_index 5, never playing the remaining frames. I"ve used floor(image_index) and that doesn't fix the problem.

I've searched around and other people have had this problem. Is there anyway to fix? this is so frustrating.

Edit: After much experimenting, I added the below code in step event. I added the floor image index == 5 because it wouldn't play past 5. This seems to work okay. Still I have to jump over a frame which I prefer not to do. Any better ways than this?
Code:
var end_frame = 7;
if ((sprite_index = spr_player_jump_w) || 
(sprite_index = spr_player_jump_e) || 
(sprite_index = spr_player_jump_n) || 
(sprite_index = spr_player_jump_s)) { 
    if (floor(image_index) == 5) {
        image_index = end_frame - image_speed;
    }
    if (image_index >= end_frame - image_speed) {
        global.jump = false;
        image_index = -1; //stops animation
        //image_index = 7; //added so shows last frame of jump
        z = 0;
        player_direction_sprite_stopped();
        jump_sound_played = false;
    }
}
 
Last edited:

TheouAegis

Member
Code:
if ((sprite_index = spr_player_jump_w) ||
(sprite_index = spr_player_jump_e) ||
(sprite_index = spr_player_jump_n) ||
(sprite_index = spr_player_jump_s)) {
    image_speed = 0;
    image_index = image_number -1;
    z = 0;
    jump_sound_played = false;
    player_direction_sprite_stopped();
edit: forgot a number
 
Last edited:

T2008

Member
Thanks for responding. I tried this in animation end event but it doesn't work, as the animation end event is never triggered. It always goes to 5 plus fraction then goes back to 1, skipping the last few frames. I have no idea why it does this.

Edit: Just now realizing that all animations are only going to image index 5, then back to zero. This is awful. Any idea as to why? I guess I don't understand how GM cycles animations, etc. I'd appreciate any insight in to this!
 
Last edited:

Pep Andorra

Member
So, I've had a lot of trouble with the animation end event. Right now, I've tested my jump code and the animation end event never triggers. Through debug, it shows that the last two frames of the sprite are never shown and I have no idea why. I can only get the animation to stop at image_index 5, never playing the remaining frames. I"ve used floor(image_index) and that doesn't fix the problem.

I've searched around and other people have had this problem. Is there anyway to fix? this is so frustrating.

Edit: After much experimenting, I added the below code in step event. I added the floor image index == 5 because it wouldn't play past 5. This seems to work okay. Still I have to jump over a frame which I prefer not to do. Any better ways than this?
Code:
var end_frame = 7;
if ((sprite_index = spr_player_jump_w) ||
(sprite_index = spr_player_jump_e) ||
(sprite_index = spr_player_jump_n) ||
(sprite_index = spr_player_jump_s)) {
    if (floor(image_index) == 5) {
        image_index = end_frame - image_speed;
    }
    if (image_index >= end_frame - image_speed) {
        global.jump = false;
        image_index = -1; //stops animation
        //image_index = 7; //added so shows last frame of jump
        z = 0;
        player_direction_sprite_stopped();
        jump_sound_played = false;
    }
}
Hello,

When comparing two values to see if they are equal, you should use the "==" operator, and only use the "=" one for assignment.

You should write:

if ((sprite_index == spr_player_jump_w) ||
(sprite_index == spr_player_jump_e) ||
(sprite_index == spr_player_jump_n) ||
(sprite_index == spr_player_jump_s)) {
 

T2008

Member
Thanks. Someone told me GM didn't care, but I agree that I should use proper form. I changed that and it still doesn't work. I'm now having trouble with my walk animation which is now always ending at frame 5 and loops back to beginning when it has 8 frames. Any ideas as to why this would be happening? I"m guessing that it thinks it should be same length as idle animation which has 6 frames, but I don't know why it would do that.

The relevant part of walking code is in movement script below. The animation scripts just set the sprite index based on direction.

Edit: I confirmed that what's happening is that it's making the walk sprite only cycle the number of images that the idle animation had; when I made the idle animation one frame, only one frame of walking was shown, even though image index was 1 (thus showing it was registering as one frame). I have no idea why it started doing this. I'm unbelievably frustrated by it.

Code:
if (move_east) {
        //---Define Direction
        Direction = "Right";
        if (!global.been_hit) && (!global.jump) && (!global.climb) {
        //---Set Sprite
        if (shooting_done) {
            if (global.walking_speed < 4) {
                player_direction_sprite_walk();
                image_speed = 1;
            }
            if (global.walking_speed >= 4) {
                player_direction_sprite_run();
                image_speed = 1;
            }
        }
        [collision code omitted]
 
//Stop East
if (stop_east) {
    if (!(keyboard_check_pressed(ord("V")))) {
        cooldown = 0;
        shooting_done = true;
        player_direction_sprite_stopped();
        image_speed = 1;
    }
}
 
Last edited:

Slyddar

Member
I'm not sure if you are having this problem, but there is a confirmed bug regarding image_index not remaining as an integer, even when it gets set to one previous step. It keeps reverting back to a real that is less than the expected value, meaning flooring it does not work. This is the example I supplied to support.
Might not be what you are experiencing, but thought it worth mentioning in case.
 

T2008

Member
Thanks. To be honest, I don't know enough to know if this is the issue. I hope someone can help me with this, as I'm not experienced at programming.

Edit: I have the fps set in the sprite editor to a different number for walking sprite as I do from idle sprite. Could that be a problem? I tried adjusting the number to where the idle sprite had higher number than walking and it may have helped. Does anyone know how the sprite editor fps affects the animations and image indexes? Still not sure if this is the problem. (Actually it didn't fix it)
 
Last edited:

TheouAegis

Member
I forgot a -1 in my code after image_number. You could try it again, but it sounds like it won't work.

And you are certain your sprite has 8 frames of animation (image_index of 7 is the 8th frame). And that you are using the right sprites?
 

T2008

Member
It looks like you did place a -1 there??? Where should the -1 go?

Yes, I'm certain about the frames. So, I was up all night and still couldn't find why it was making my walk sprite have same frames as idle. I did find a very undesirable of adding two frames to the idle animation and the walk now plays correctly. However, my concern is what about other sprites I have that are longer than the idle and walk? They might break and I can't modify them or add any more to idle. I really would like to know the problem.
 

TheouAegis

Member
It looks like you did place a -1 there??? Where should the -1 go?
I edited it in this morning. it wasn't in the original post.

Are you remembering to set image_index to 0 every time you change the sprite? Sprites should not affect each other like that at all.
 

T2008

Member
Here is a sample of my movement code for east. The problem is that I can't set to zero or it will only play 0 all the time. Any suggestion as to how to do this?

Code:
move_east        = (keyboard_check(vk_right)) && !(keyboard_check(vk_up)) && !(keyboard_check(vk_down)) && !(keyboard_check(vk_left));

if (move_east) {
        //---Define Direction
        Direction = "Right";
        if (!global.been_hit) && (!global.jump) && (!global.climb) { //i added
        //---Set Sprite
        if (shooting_done) {  
            if (global.walking_speed < 4) {
                player_direction_sprite_walk();
                image_speed = 1;
            }
            if (global.walking_speed >= 4) {
                player_direction_sprite_run();
                image_speed = 1;
            } 
        } 
        //---Collisions That Set Walking Speed To Zero
        [omitted]
        //---Move Player
        x += global.walking_speed;
        }
    }
//Stop East
if (stop_east) { 
    if (!(keyboard_check_pressed(ord("V")))) {
        cooldown = 0;
        shooting_done = true;
        player_direction_sprite_stopped();
        image_speed = 1;
        walking = false;
    }
}
 

Nidoking

Member
Is it possible that you're setting sprite_index multiple times per step? Maybe you're setting it to a sprite that has fewer frames, causing image_index to reset, and then setting it back to the previous sprite before a draw event.
 

T2008

Member
When the movement key is pressed (keyboard_check), it sets sprite to run or walk depending on speed; then when keyboard check released it sets sprite to stopped. Is there an issue with this? Is there a way I could write this to where it does it only once for walk/run sprite?
 

TheouAegis

Member
I would debug that sprite_index (somehow). Every time you change the sprite, show a debug message. Regardless of whatever you could do to make a coat better, we need to First figure out why it is not good. What Nidoking said is at this point the most logical source of the problem. So we are basically both suggesting you track your sprite index constantly and see if it is ever-changing where it shouldn't be. For example, if you see it constantly flashing the running Sprite and walking sprite then running sprite then walking sprite, or Run Sprite and Idle sprite then run sprite then idle Sprite, as the case sounds to be, then that is going to throw off your image indexing just like Nidoking said.
 

T2008

Member
Thanks. I'll try to debug this somehow. I think it might have something to do with the keycheck not being a one time thing. I don't know though.

Edit: I keep going through my code. The only places that I have sprite changes involve press key to attack, shoot, or interact, and also when hit. The sprite changes only occur at time of pressing key (or being hit). When I run the game, none of theses things are happening, which tells me it must be related to my walking code that I posted above. Any ideas as to how to fix my code above? I've tried created variables, etc to only run sprite script under various circumstances and nothing works.
 
Last edited:

T2008

Member
Well, I found the issue. I had the user interface adjust sprite when equipping items, so this somehow messed up the sprite changes, etc. Now, I have to figure out a way to change weapons and immediately update the sprite without ruining the sprite animations.
 
D

Deleted member 13992

Guest
Always floor(image_index) if you are checking a direct comparison == to an integer. Lines like this one will rarely work reliably: (sprite_index = spr_player_jump_w) because a lot of the time, image_index will be a non-integer value.

The only exceptions for this are if you are checking for ranges with >= or <=, or if you have completely stopped the animation speed and are flipping through sprite frames manually through code.

EDIT: Confused sprite_index with image_index. My mistake, feel free to disregard. I see image_index misused all the time so I jumped to that conclusion too quickly.
 
Last edited by a moderator:

TheouAegis

Member
Always floor(image_index) if you are checking a direct comparison == to an integer. Lines like this one will rarely work reliably: (sprite_index = spr_player_jump_w) because a lot of the time, image_index will be a non-integer value.

The only exceptions for this are if you are checking for ranges with >= or <=, or if you have completely stopped the animation speed and are flipping through sprite frames manually through code.
Did you actually read what you quoted? Your whole reply was unnecessary because it had nothing to do with what you referenced.
 

T2008

Member
Always floor(image_index) if you are checking a direct comparison == to an integer. Lines like this one will rarely work reliably: (sprite_index = spr_player_jump_w) because a lot of the time, image_index will be a non-integer value.

The only exceptions for this are if you are checking for ranges with >= or <=, or if you have completely stopped the animation speed and are flipping through sprite frames manually through code.
Thanks for responses.
The sprite_index == spr_player_jump_w is relating to sprite index and not image index. It makes sense to floor the image index. But why would I floor image index relating to the sprite index??? Why would the sprite index line not work reliably??

I fixed the last remaining issue I had relating to the sprites and everything is working. For anyone who might be reading, when I placed the sprite change on the key release event in the user interface object from a different spot, it worked perfectly. I know I didn't post this code as it is way too long (and original post was related to jump), but hope this is enough to help someone with a similar issue. Thanks everyone who tried to help! It's nice to have people to give insight.
 
Top