GameMaker Need some help with a button

U

User

Guest
I want to make a button. It will be animated button.
When user do nothing - button look like simple image.
When user move mouse on button - it will be animation "backlighting".
When user move mouse out button - it will be animation "fogging".
All animations must stop on last frame.
I have 3 sprites.

Sprite 1 - "button_normal_sprite". This is a button when no one touches it. Initial button state.


Sprite 2 - "button_mouse_on_sprite". This is a button when user move mouse on button.


Sprite 3 - "button_mouse_leave_sprite". This is a button when user leave mouse on button.


I create a object, load a "button_normal_sprite" and write a code.
Events->Step
Code:
//Mouse on button - play animation "backlighting".
//It work good
if (instance_position(mouse_x,mouse_y,id)>0)
{
sprite_index=button_mouse_on_sprite;
if (image_index>4) image_speed = 0; //Stop on last(?) frame
}


//Mouse out of button - play animation "fogging".
//Problem here...
/*
if (instance_position(mouse_x,mouse_y,id)<0)
{
sprite_index=button_mouse_leave_sprite;
if (image_index>4) image_speed = 0;
}
*/
What code should I write?

PS
I dont use mouse enter-leave events. In this case they can be lagging (a lot of videos on youtube "gms2+create buttons" where people show it).
 
S

SkSonicSk

Guest
Well, in your case you have three different situations:
The second part of code fits for two situations ( user do nothing and move out button)

I suggest you create some variable to control if button was actived. With this, you can differ a user moving out and a user doing nothing.

Something like that:

Code:
buttonenter=false;

if (instance_position(mouse_x,mouse_y,id)>0)
{
sprite_index=button_mouse_on_sprite;
if (image_index>4) image_speed = 0;
buttonenter=true;
}


if (instance_position(mouse_x,mouse_y,id)<0)
{
if (buttonenter){
sprite_index=button_mouse_leave_sprite;

if (image_index>4) {
image_speed = 0;
buttonenter=false;
}
}else{
sprite_index=button_normal_sprite
}

}
 
Are the enter and leave sprites the same thing but reversed? Might as well save on the memory and just have all three sprites as one strip going from darkest to lightest and then go in the desired direction. The code would then essentially look like this:

Code:
if (instance_position(mouse_x,mouse_y,id))
    index = min(index+speed,5); // or 4, if the default sprite and the first frame of the enter sprite are the same
else if (index)
   index -= speed;
 
Top