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

GameMaker [SOLVED] Ranged Weapon Character Sprite Not Changing

S

Sarena

Guest
I created a system where you can switch between two different characters, one that has a ranged weapon and one that uses melee attacks.

The animations for the melee character are showing correctly, but the ranged character animations are not.

The ranged character is using a weapon like a slingshot, so I want it to play their animation, then the projectile should shoot at the target. Right now, the projectile shoots, but the animation doesn't play.

Here is the portion of objPlayer's Step Event, related to the ranged weapon:
Code:
        //Evelyn Attack
    
        // Assign Variable
        firing_delay = firing_delay - 1;
    
        // If Expression
        if(firing_delay < 0)
        {
            // Assign Variable
            eattack = true;
        
            // Assign Variable
            firing_delay = 1;
        
            // Create Instance
            instance_create_layer(x, y, "Projectile", objProjectile);
        
            // If Expression
            if(eattack)
            {
                // Assign Variable
                moving = false;
            
                // If Expression
                if(direction_facing = 1)
                {
                    // Set Sprite
                    sprite_index = sprPEvelyn_RAttack;
                    image_index += 0;
                
                    // If Expression
                    if(sprite_index = sprPEvelyn_RAttack)
                    {
                        // If Expression
                        if(image_index = 8)
                        {
                            // Set Sprite
                            sprite_index = sprPEvelyn_Right;
                            image_index = 0;
                        
                            // Assign Variable
                            eattack = false;
                        }
                    }
                }
            
                // If Expression
                if(direction_facing = 2)
                {
                    // Set Sprite
                    sprite_index = sprPEvelyn_UAttack;
                    image_index += 0;
                
                    // If Expression
                    if(sprite_index = sprPEvelyn_UAttack)
                    {
                        // If Expression
                        if(image_index = 8)
                        {
                            // Set Sprite
                            sprite_index = sprPEvelyn_Up;
                            image_index = 0;
                        
                            // Assign Variable
                            eattack = false;
                        }
                    }
                }
            
                // If Expression
                if(direction_facing = 3)
                {
                    // Set Sprite
                    sprite_index = sprPEvelyn_LAttack;
                    image_index += 0;
                
                    // If Expression
                    if(sprite_index = sprPEvelyn_LAttack)
                    {
                        // If Expression
                        if(image_index = 8)
                        {
                            // Set Sprite
                            sprite_index = sprPEvelyn_Left;
                            image_index = 0;
                        
                            // Assign Variable
                            eattack = false;
                        }
                    }
                }
            
                // If Expression
                if(direction_facing = 4)
                {
                    // Set Sprite
                    sprite_index = sprPEvelyn_DAttack;
                    image_index += 0;
                
                    // If Expression
                    if(sprite_index = sprPEvelyn_DAttack)
                    {
                        // If Expression
                        if(image_index = 8)
                        {
                            // Set Sprite
                            sprite_index = sprPEvelyn_Down;
                            image_index = 0;
                        
                            // Assign Variable
                            eattack = false;
                        }
                    }
                }
            }
        }
    }

    // Else
I also set up a system where depending on the direction you are facing, the player will face and attack in that direction. I set up numbers to coordinate with each direction in the create event.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
To me, it seems that the code should be in two parts... One where you set the sprite to use for shooting, and then the other, where you check the image index to reset the sprite. From what I see of your code at the moment, it will not do either of these things correctly... Rather than try to explain line for line what you may or may not be doing wrong, I've re-written the code to look like I think it should from what t seems like you are doing:

Code:
//Evelyn Attack
// If Expression
if(--firing_delay < 0)
{
// Assign Variable
eattack = true;
// Assign Variable
firing_delay = 1; // Only 1 step for the fireing delay? Could this be the issue?
// Create Instance
instance_create_layer(x, y, "Projectile", objProjectile);
// Assign Variable
moving = false;
// If Expression
image_index = 0;
switch (direction_facing)
    {
    case 1: sprite_index = sprPEvelyn_RAttack; break;
    case 2: sprite_index = sprPEvelyn_UAttack; break;
    case 3: sprite_index = sprPEvelyn_LAttack; break;
    case 4: sprite_index = sprPEvelyn_DAttack; break;
    }
}

if eattack
{
if image_index >= 8 // GMS uses floats, so better to check if you are equal-to OR greater-than a value
    {
    // Set Sprite
    switch (sprite_index)
        {
        case sprPEvelyn_RAttack: sprite_index = sprPEvelyn_Right; break;
        case sprPEvelyn_UAttack: sprite_index = sprPEvelyn_Up; break;
        case sprPEvelyn_LAttack: sprite_index = sprPEvelyn_Left; break;
        case sprPEvelyn_DAttack: sprite_index = sprPEvelyn_Down; break;
        }
    image_index = 0;
    // Assign Variable
    eattack = false;
    }
}
Now, the thing that still seems wrong here is the firing_delay value... you only set it to 1, which means that every second step the code will run, resetting the sprite and so not showing the animation correctly. You probably want to change this value to something like room_speed so the code is only triggered once every second or something...

Hope that helps!
 
S

Sarena

Guest
Thank you so much for your help, Nocturne!

Splitting this code in two was able to really help me resolve this. To resolve it entirely, I dragged the second half of the code (checking if the animation has finished playing) down to my animation section.

I checked if the attack animation was playing and then placed all of my walk movements in an else statement between it so that it could check that first.

I think what was happening was that the walk animation would override the attack animation since it came second, so reordering them was able to allow for the actual attack animation to show.
 
Top