GML Change sprite on a SPECIFIC image index

N

Naal J.

Guest
Greetings all,

I've been dabbling with Game maker for about a month now and in a miraculous way, coding is actually starting to make sense to me, even to the point that I recently wrote some code without the use of any tutorials :)

However, I've come across an issue in which it seems to be multiple solutions to (in theory), but I do not think I'm using any of them the right way.

I have a snail like animation in which my player's way of moving is to slide across the floor. I have constructed the sprites in a manner that allows for the animation to move without moving and then I put a code in the animation end event that tells the sprites to move at the end of the animation which allows the sprites to line up seamlessly. Yet, when I press an arrow key to make the player move in another direction the sprite jerks in that direction in an unnatural way if the current sprite is not on a specific image index.

I am now trying to find a way to make the sprite change only when it has reached a specific image index on the current sprite to allow for a smooth transition. Here is the current code I was using to achieve this:

CREATE Event:

//varialbes

moving = false;
target_x = x;
target_y = y;
image_speed = 0.25;
sprite_previous = 0;


STEP Event:

//Move Player

if (target_x > x) { x += 32 } //right
if (target_x < x) { x -= 32 } //left

//Check Destination
if ( target_x == x && target_y == y ) {

moving = false;


sprite_previous = sprite_index

}


//Controls

if keyboard_check (vk_left)

{
if image_index > image_number -1
{
image_speed = 1;
sprite_index = spr_PlayerWalkLeft;
}
}



if keyboard_check (vk_right)

{
if image_index > image_number -1
{
image_speed = 1;
sprite_index = spr_PlayerWalkRight;
}
}


ANIMATION END Event:

if sprite_index = spr_PlayerWalkLeft {

target_x -= 32;

}

if sprite_index = spr_PlayerWalkRight {

target_x += 32;

}


This technique will only work if I press an arrow key at the exact moment the requested image is on the screen. If it's not on the screen, the player just continues with it's current animation. I want to make so that if you press the right arrow key while the player is going left, the sprites changes directions only when it reaches a SPECIFIC image_index. I think its also best that the player does not accept any further commands until it has successfully changed directions. Previously I tried to use alarms that go off when a key is pressed (which got extremely messy) and before that I experimented with switch statements, but I do not think I was using them correctly. Any push in the right direction would be highly appreciated. Let me know if more information is needed.
 

YoSniper

Member
Here's what I would do:

I would create a state variable that would determine the player's next animation when his image_index got to a high enough value. This state would reset afterwards. This way, you wouldn't need to time your key presses.

Create Event
Code:
moving = false;
target_x = x;
target_y = y;
image_speed = 0.25;
sprite_previous = 0;
next_move_state = "NONE";
Step Event
Code:
//Move Player
if (target_x > x) { x += 32 } //right
if (target_x < x) { x -= 32 } //left

//Check Destination
if ( target_x == x && target_y == y ) {
    moving = false;
    sprite_previous = sprite_index;
} 

//Controls
if keyboard_check(vk_left) {
    next_move_state = "LEFT";
} else if keyboard_check(vk_right) {
    next_move_state = "RIGHT";
}

//Change sprite depending on state
if image_index > image_number - 1 {
    if next_move_state == "LEFT" {
        image_speed = 1;
        sprite_index = spr_PlayerWalkLeft;
    } else if next_move_state == "RIGHT" {
        image_speed = 1;
        sprite_index = spr_PlayerWalkRight;
    }
    //Reset state
    next_move_state = "NONE";
}
Animation End Event
Code:
if sprite_index = spr_PlayerWalkLeft {
    target_x -= 32;
}

if sprite_index = spr_PlayerWalkRight {
    target_x += 32;
}
 
N

Naal J.

Guest
@YoSniper Wow! Had I known the response would have been so fast, I would have hung around. Thanks for helping me out. The variable/ code you gave me seems to be doing it's job, however I believe the jerking motion I was referring to earlier is caused by the animation end event:

So what I think is happening is at the end of the animation it's moving in the direction of the next move state. In order for the animation to line up properly, I need the current sprite_index to move 32 more pixels in the direction it's already going one more time or I need the new sprite to reset 32 pixels in front of the previous sprite index.

Does that make any sense? I guess I basically need a way to check the conditions of the sprite_index and the next_move_state (without it contradicting with the existing code), so I can tell the sprite how many pixels it should move in either direction before calling on the animation end event.
 
Last edited by a moderator:

Yal

šŸ§ *penguin noises*
GMC Elder
The animation event just happens when the animation rolls over from the last image to the first, you can't really postpone it. (You can call it whenever you want with event_perform but I don't think this is what you want to do in this case).

If you use a state machine approach, you could have a special "turn around" state that you transit into when turning around, though... or maybe you could just check what direction you're going and then only change the sprite when you're at the right image to align it?
 
Top