Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Stranga on Mon 07/11/2016 11:48:49

Title: [SOLVED] Main Menu Play button Animated when Selected
Post by: Stranga on Mon 07/11/2016 11:48:49
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.
Title: Re: Main Menu Play button Animated when Selected
Post by: Stranga on Mon 07/11/2016 12:02:01
Wait never mind I found out how to do it in my function btnPlayGame_OnClick.

Sorry everyone
Title: Re: Main Menu Play button Animated when Selected
Post by: Crimson Wizard on Mon 07/11/2016 12:09:57
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) Select

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 (http://www.adventuregamestudio.co.uk/wiki/GUI_Button_functions_and_properties#Button.Animate), and animation is stopped by setting Button.NormalGraphic.
Code (ags) Select

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) Select

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;
        }
    }
}