1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Change alpha overtime on mouseover.

Discussion in 'Programming' started by L0v3, Jun 27, 2016.

  1. L0v3

    L0v3 Guest

    Working on tweening highlight effect for mouseover on object, in my scenario to be used for changing the drawing alpha of an object on mouseover, will make more generic later.

    The below code works perfectly fine for this, however, if you leave/enter the mouse before the tween have finished, you will start the fadeout effect as if it were finished.

    Example; on mouseover it will fade the object in over 2 seconds, at 1 second you leave the mouse, and when doing so, the fade out effect will cause the alpha to jump from half faded in to as if it were fully opaque, and then start fading out. This causing some annoying blinking effects.

    Heres the code:
    Code:
    //Alpha Highlighting.
    if position_meeting(mouse_x, mouse_y, self) and !complete_fadein //Mouseover
    {  
        //Makes fadeout incomplete.
        complete_fadeout = false;
     
        //Mouseover Fade In
        image_alpha = tween_linear(step, duration, lower, upper);
        step++;
     
        //Check if complete.
        if (step > duration)
        {
            complete_fadein = true;
            step = 0;
        }
    }
    else if !position_meeting(mouse_x, mouse_y, self) and !complete_fadeout //No Mouseover
    {
        //Makes fadein incomplete.
        complete_fadein = false;
     
        //No Mouse Fade Out
        image_alpha = tween_linear(step, duration, upper, lower);
        step++;
     
        //Check if complete.
        if (step > duration)
        {
            complete_fadeout = true;
            step = 0;
        }
    }
    

    What I am looking for is a way for this not to occur. My ideas have been taking a snapshot of some variables when mouse leaves, but not quite sure how to implement this. Have experimented to no avail, and raised alot more questions instead. Been tunnel visioned most of day on this so decided to make a topic.
     
  2. Jaqueta

    Jaqueta Member

    Joined:
    Jun 23, 2016
    Posts:
    338
    Well, you could use a simple tween formula, like this:
    x+=(target_x-x)*0.25

    It's not a linear tween, is like a ease_out one, but the it's good because It requires very little code:
    Code:
    var target_alpha=lower+(position_meeting(mouse_x, mouse_y, self)*upper);
    image_alpha+=(target_alpha-image_alpha)*0.25 //Change the 0.25 to make it tween slower or faster
     
  3. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Does tween_linear(step, duration, upper, lower) return lower + (upper - lower) * step / duration?

    If so, there are many simple ways to do this.
    One way is to calculate the difference in each step and work with that (this is what I recommend).
    An other way is to set step to (duration - step) when switching from fading in to fading out.
     
  4. Jaqueta

    Jaqueta Member

    Joined:
    Jun 23, 2016
    Posts:
    338
    And if you really want a linear tween, here's your code:

    Code:
    var step_add=position_meeting(mouse_x, mouse_y, self);
    if step_add=0 then step_add=-1 //If the position meeting returns false, it set the value to -1
    step+=step_add*0.1 //In this case I want to change only 0.1 per step
    step=clamp(step,0,1) //Define a min and max value for the Step, in this case 0 and 1
    image_alpha = lerp(lower,upper,step); //And finally set the image_alpha, I used lerp because I don't have your tween script
     
  5. L0v3

    L0v3 Guest

    I already have plenty of tweens Jaqueta, that's not the problem. Yes jo-thijs that is what it returns, I was thinking of finding a delta value aswell, but not exactly sure where and how to do this and on what variables. Can you give me an example or specify more?
     
  6. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Code:
    var d = (upper - lower) / duration;
    if !position_meeting(mouse_x, mouse_y, self)
        d = -d;
    
    image_alpha = clamp(image_alpha + d, lower, upper);
     
  7. Jaqueta

    Jaqueta Member

    Joined:
    Jun 23, 2016
    Posts:
    338
    Well, I don't know how to put this in words, but I can put this in an example...
    Perhaps I didn't understand what your problem is? ;P
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice