[SOLVED] Main Menu Play button Animated when Selected

Started by Stranga, Mon 07/11/2016 11:48:49

Previous topic - Next topic

Stranga

Hello Everyone,

Sorry if I seem a pain but I have no clue on how to achieve this. I've searched the forums and still can't get this to work. What I want to achieve is to animate a menu select button when it is selected or even mouse over the button.

Any help will be muchly appreciated.

Stranga

Wait never mind I found out how to do it in my function btnPlayGame_OnClick.

Sorry everyone

Crimson Wizard

#2
EDIT: well, I was writing a long post about how to animate button when mouse is over, and I do not want to discard it :).



When solving complex problems, divide it in smaller ones. You need to know:
1) how to detect when mouse is over button;
2) how to animate button;
3) how to change button visual state when mouse moves in and out.


1. You can find out which control mouse is over using GUIControl.GetAtScreenXY:
Code: ags

GUIControl *gc= GUIControl.GetAtScreenXY(mouse.x, mouse.y);
// Check if this is button, and not just any button, but the one we want --
if (gc.AsButton == myButton)
{
    // mouse is over myButton, so do something
}



2. Button is animated using Button.Animate, and animation is stopped by setting Button.NormalGraphic.
Code: ags

GUIControl *gc = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
// Check if this is button, and not just any button, but the one we want --
if (gc.AsButton == myButton)
{
    // mouse is over myButton, animate
    gc.AsButton.Animate(VIEW, LOOP, DELAY, eRepeat);
}
else
{
    // mouse is not over myButton anymore, reset animation
    gc.AsButton.NormalGraphic = NORMAL_BUTTON_GRAPHIC;
}




3. Where to put this code, so that it will run tests in real time? In repeatedly_execute, or even better repeatedly_execute_always, because GUI can pause game, and repeatedly_execute does not work when game is paused.
However, you also need not restart animation every moment the mouse is over button, so you need to also remember animation state.
Code: ags

bool mouse_was_over_mybutton;

function repeatedly_execute_always()
{
    GUIControl *gc = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    // Check if this is button, and not just any button, but the one we want --
    if (gc.AsButton == myButton)
    {
        // mouse is over myButton, animate
        if (!mouse_was_over_mybutton)
        {
            gc.AsButton.Animate(VIEW, LOOP, DELAY, eRepeat);
            mouse_was_over_mybutton = true;
        }            
    }
    else
    {
        // mouse is not over myButton anymore, reset animation
        if (mouse_was_over_mybutton)
        {
            gc.AsButton.NormalGraphic = NORMAL_BUTTON_GRAPHIC;
            mouse_was_over_mybutton = false;
        }
    }
}

SMF spam blocked by CleanTalk