Legacy GM Any advice for animation code?

G

graviax

Guest
Hi,
I'm pretty new in gamemaker, and since I'm **** at programming animation I wanted to ask your methods to do it.
What do you prefer ect...
 

johnwo

Member
Use a state machine.

Something along the lines of:
Code:
// Create event
enum states {
    idle,
    walk,
    jump
};

currentState = states.idle;

// Step event
switch (currentState) {
    case 0: // Idle
    if sprite_index != spr_idle then {
        sprite_index = spr_idle;
        image_index = 0;
        image_speed = 1;
    }
    break;
    case 1: // Walk
    if sprite_index != spr_walk then {
        sprite_index = spr_walk;
        image_index = 0;
        image_speed = 1;
    }
    break;
    case 2: // Jump
    if sprite_index != spr_jump then {
        sprite_index = spr_jump;
        image_index = 0;
        image_speed = 1;
    }

    // Freeze jumping animation on the last frame
    if floor(image_index) == image_number - 1 then
        image_speed = 0;
    break;
}
Then you just set currentState to states.idle, states.walk, states.jump at the appropriate places.

Cheers!
 
G

graviax

Guest
Use a state machine.

Something along the lines of:
Code:
// Create event
enum states {
    idle,
    walk,
    jump
};

currentState = states.idle;

// Step event
switch (currentState) {
    case 0: // Idle
    if sprite_index != spr_idle then {
        sprite_index = spr_idle;
        image_index = 0;
        image_speed = 1;
    }
    break;
    case 1: // Walk
    if sprite_index != spr_walk then {
        sprite_index = spr_walk;
        image_index = 0;
        image_speed = 1;
    }
    break;
    case 2: // Jump
    if sprite_index != spr_jump then {
        sprite_index = spr_jump;
        image_index = 0;
        image_speed = 1;
    }

    // Freeze jumping animation on the last frame
    if floor(image_index) == image_number - 1 then
        image_speed = 0;
    break;
}
Then you just set currentState to states.idle, states.walk, states.jump at the appropriate places.

Cheers!
so simple yet it changed my life
thx dude
 
Top