Enemy changes sprites

firestar

Member
When the enemy in my project spawns in the air it is the actual enemy sprite but when it falls down it changes sprites to the jumping animation and it's not even the enemy jump animation :(

Can anyone help with this?
 

Nidoking

Member
From this amount of information, the comprehensive answer is "Find why the sprite is changing and then make it not do that."

If you provide some actual information about your game, such as posting whatever you do with the sprites, more feedback may be possible.

See: How to ask better questions.
 
....but when it falls down it changes sprites to the jumping animation and it's not even the enemy jump animation :(

Can anyone help with this?
That's probably because your code says IF it's not in contact with ground/solid then it is to assume it's to jump animate - which is what you're seeing because of where you're spawning it. So to solve it, you could maybe add another collider for that enemy object to detect when it should actually change to jumping and when to retain its default animation, i.e. at the edge of a ledge.
 

firestar

Member
That's probably because your code says IF it's not in contact with ground/solid then it is to assume it's to jump animate - which is what you're seeing because of where you're spawning it. So to solve it, you could maybe add another collider for that enemy object to detect when it should actually change to jumping and when to retain its default animation, i.e. at the edge of a ledge.
I have done this, but I do not know what is wrong with it because I went over it many times:

GML:
vsp = vsp + grv;

//Horizontal collision
if(place_meeting(x+hsp, y, coolObjectWall))
{
    while (!place_meeting(x+sign(hsp), y, coolObjectWall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;

//Vertical collision
if (place_meeting(x, y+vsp, coolObjectWall))
{
    while (!place_meeting(x, y+sign(vsp), coolObjectWall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;


//Animation
if (!place_meeting(x, y+1, coolObjectWall))
{
    sprite_index = coolEnemyJump;
    image_speed = 0;
    if (sign(vsp) > 0) image_index = 1; else image_index = 0; 
}
else
{
    image_speed = 1;
     if (hsp == 0)
    {
        image_speed = 1;
        sprite_index =    coolEnemyGuy;
    }
    else
    {
        image_speed = 1;
        sprite_index = coolEnemyRun;
    }
}

if (hsp != 0) image_xscale = sign(hsp);
Also this code is based off the player movement
 
To put it bluntly, you're blindly copy pasting and simply changing a bit here and there the code originally from your objPlayer, as in not really understanding what each line of code is meant to do. Your above code is doing what it has been programmed to do as you have coded in the above, and seen while running the game you are making. No where in there says anything "hey if my enemy is spawned off the ground (not touching the ground) make it retain its default animation (sprite), don't assume it is jumping when it is actually falling/being pulled down by grv (gravity)". In fact, I am very very sure this sprite "coolEnemyRun" with the code looking like the above, it is never executed, why? because for it to execute, it needs the hsp to be greater than 0 (go right) or less than 0 (go left). Likewise your enemy when it is touching the ground (again with just the above code) it will never use "coolEnemyJump", because the vsp needs to counteract the grv value otherwise if you try remove "coolObjectWall" from under it, it'd continue to fall off your screen. Your above code doesn't change the value of the vsp to allow it to trigger jump. The reason it works on your objPlayer is because the hsp and vsp are being managed by the player/user input (keyboard/mouse/gamepad), but in the case of your enemy character there's none such external intervention. So either you adjust your code accordingly, or you add another checker (collider/trigger) to make it distinguish between falling, jumping, idle, walking, running, floating, etc.
 
Top