SOLVED Changing Sprite (Animated ON/OFF Button)

Telorr

Member
Hi guys, I'm a newbie here
I need your help

I want make a object (oButton) which when I click it in OFF condition, it will change to ON condition and vice versa.
But, i can't make it. I can't set the animation precisely. It will loop continuously.
This sprite has a total of 24 frames. Frame 1 for OFF condition and frame 24 for ON condition.
I want the default condition is OFF (frame 1) and will change to ON (frame 24) when I click this button.

Help me, Mastah!
Thank you
 

Slyddar

Member
To clarify, do you want the frames in between to play through each time you switch it, or is it frame 1 is set when it's off, and frame 24 will be set when it's on, with no animation in between?
 

Telorr

Member
Yes, I want the animation play through each time I switch it.
There is an animation between frame 1 and 24.
 

Slyddar

Member
There is an animation between frame 1 and 24.
So does the animation from 1 to 24 to turn it on, need to play in reverse to turn it off?
On is the animation from 1 to 24, and stops at 24. Off is the animation from 24 to 1 and stops at 1. Just need clarification before advising the best way.
 
Hmm, my approach would be the following:
GML:
// CREATE EVENT
animate = false;
animation_direction = 1;
frame = 0;

// STEP EVENT
if (trigger_button_conditional) {

    animate = true; // Once the button is set to be triggered, then we turn on the 'animate' variable

    // We'll assume that if not at frame 0 when triggered, it should go to 0
    animation_direction = 1;

    if (frame > 0) {
        animation_direction = -1;
    }

    trigger_button_conditional = false; // You have to turn off the conditional, otherwise this code will be executed many times!
   // You can also change the conditional of this block to be 'if (trigger_button_conditional && !animate) {...}', in case you don't want to turn off the conditional
}


if (animate) { // When the button is set to animate, change frames

    // Switch to the next frame
    frame += animation_direction;


    if (frame == 0 || frame == sprite_get_number(sprite_index)) { // The animation has reached its end
   
        // End animation (note that sprite_get_number(sprite_index), in your case, should return 24, but a generic code is preferable, in my opinion)
        animate = false;
    }

}

Then, you only would have to draw using the variable 'frame' as the subimage index. This is a fast-made code, so some optimitzation may be required. Hope it helps!
 
Last edited:

Telorr

Member
So does the animation from 1 to 24 to turn it on, need to play in reverse to turn it off?
On is the animation from 1 to 24, and stops at 24. Off is the animation from 24 to 1 and stops at 1. Just need clarification before advising the best way.
Yes it is. Sorry for late reply
 

Telorr

Member
Hmm, my approach would be the following:
GML:
// CREATE EVENT
animate = false;
animation_direction = 1;
frame = 0;

// STEP EVENT
if (trigger_button_conditional) {

    animate = true; // Once the button is set to be triggered, then we turn on the 'animate' variable

    // We'll assume that if not at frame 0 when triggered, it should go to 0
    animation_direction = 1;

    if (frame > 0) {
        animation_direction = -1;
    }

    trigger_button_conditional = false; // You have to turn off the conditional, otherwise this code will be executed many times!
   // You can also change the conditional of this block to be 'if (trigger_button_conditional && !animate) {...}', in case you don't want to turn off the conditional
}


if (animate) { // When the button is set to animate, change frames

    // Switch to the next frame
    frame += animation_direction;


    if (frame == 0 || frame == sprite_get_number(sprite_index)) { // The animation has reached its end
 
        // End animation (note that sprite_get_number(sprite_index), in your case, should return 24, but a generic code is preferable, in my opinion)
        animate = false;
    }

}

Then, you only would have to draw using the variable 'frame' as the subimage index. This is a fast-made code, so some optimitzation may be required. Hope it helps!
This code give me an overview.
I changed few line from your code and it worked perfectly.
Thank you very much for your help!
 
Top