• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows sprite_index not working properly

A

Ammaccabanane

Guest
So i just bought gms2 and started a new project.
as i started inserting animations i noticed something. Unlike in gm1 if i set sprite_index = 0 it just locks on set index

now here is my draw event:

Code:
draw_self();

show_debug_message("image_index = "+ string(image_index));

if hsp_ == 0
{
    if vsp_ == 0
    {
        if crouch
        {
            if sprite_index == spr_mm_idle
            {
                image_index = 0;
                sprite_index = spr_mm_crouch;
                image_speed = 1;
                
                show_debug_message("image_index = "+ string(image_index));
            }
        }
        else
        {
        image_index = 0;
        sprite_index = spr_mm_idle;
        image_speed = 1;
        show_debug_message("image_index = "+ string(image_index));
        
        }
    }
}

if hsp_ != 0 {
    image_index = 0;
    sprite_index = spr_mm_walk;
    image_speed = 1.5;
    show_debug_message("image_index = "+ string(image_index));
    
    
}


now the funny part is that it draws spr_mm_crouch correctly and the index goes up and resets, but for all the other animations it doesnt work at all, it just displays the first frame frozen and prints that set frame. I've checked all the animations over and over and i cant seem to find any differences, so i checked the code and found no problems. Then i tried to swap out spr_mm_crouch with any other one and it didnt work either so i really dont know what to do. I'm a newbie but if you know why this is happening please help D:
 
Because every step you're setting the image index to 0. The if statements return true every time. You may wish to use states to overcome this rather than testing the sprite index.

It works on crouching because you only set the image index to 0 here when the sprite index is idle. When the next step occurs you're crouching, not idle, so it doesn't return true.

The sprite_index is working correctly ;)
 

Kaliam

Member
Just to be thorough, the problem your having is when you set the image index to 0

Code:
image_index = 0;
You could do a quick fix to see that image_index and sprite_index do work just by deleting the lines that are setting the image index to 0. This is what is causing your sprite to be "stuck" on a frame and never animate.

If if you look at your hsp_ and vsp_ variables, the only time it's not going to set the image index to 0 is when hsp_ == 0 AND vsp_ != 0. So in other words, there is only one very specific situation where your code isn't going to be setting image_index to 0, so chances are your code is going to be always setting the index to 0 which will freeze your animation every single step.

Try creating a flow chart or a diagram to help you understand what is going on here. It will also help you with the logic of your code by going through it step by step so you can see what's actually happening.

Simple features such as sprite_index and image_index rarely have issues, so chances are it's most likely a problem with how they are being used.
 
Last edited:
Top