• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Change alpha overtime on mouseover.

L

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.
 
J

Jaqueta

Guest
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
 

jo-thijs

Member
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.
 
J

Jaqueta

Guest
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
 
L

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?
 

jo-thijs

Member
Code:
var d = (upper - lower) / duration;
if !position_meeting(mouse_x, mouse_y, self)
    d = -d;

image_alpha = clamp(image_alpha + d, lower, upper);
 
J

Jaqueta

Guest
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
 
Top