• 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!

[SOLVEDx2] Fade in and out text repeatedly

Sk8dududu

Member
I'm trying to get a sort of "blinking" text effect, except the text fades in and out. I have the code to fade the text in already, and its working fine, and I've been messing around but I cannot get it to fade out after 1 second at the same speed it faded in, and of course to continue to repeat that action.
This is my code for the create event of my text controller.
Code:
//Variables
draw = 0;
text_alpha = 0;
fadeSpeed = 0.005;
//Set alarm
alarm_set(0,300);
alarm 0 event
Code:
///Start fade
draw = 1;
I have alarm 0 starting at 300 steps because that is when I want the text to start fading in, not right when the room starts.

step event
Code:
//fade in / out
if (draw && text_alpha < 1)
{
    text_alpha += fadeSpeed;
}
and of course the draw event
Code:
//Draw "Presents"
if (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Presents");
}
what do I need to add to fade out and then continue to repeat this until i destroy the object?
 
S

SSJCoder

Guest
Init
Code:
//Variables
draw       = 0;
text_alpha = 0;
fadeSpeed  = 0.005;
fadeSign   = 1;

//Set alarm
alarm_set(0,300);
Step
Code:
//fade in / out
if (draw)
{
    text_alpha += fadeSign * fadeSpeed;
  
    // don't let alpha exceed range of 0-1
    text_alpha = max( 0, min( 1, text_alpha ) ); // * fixed
}

if ( text_alpha == 1 )
    fadeSign = -1;

if ( text_alpha == 0 )
    fadeSign = 1;
 
Last edited:

Dupletor

Member
Alarms are only executed once, so replace the alarm with itself at its end. This makes a recursion of repetition until the Alarm event no longer exists.

Alarm event:
draw = 1;
alarm[0] = 300;


I would not recommend using alarms though.

To fade out after a time, make two alarm events?

Alarm 0 event:

draw = 1;
alarm[1] = 300;

Alarm 1 event:

draw = 0;
alarm[0] = 300;
 

Sk8dududu

Member
Init
Code:
//Variables
draw       = 0;
text_alpha = 0;
fadeSpeed  = 0.005;
fadeSign   = 1;

//Set alarm
alarm_set(0,300);
Step
Code:
//fade in / out
if (draw)
{
    text_alpha += fadeSign * fadeSpeed;
  
    // don't let alpha exceed range of 0-1
    text_alpha = max( 0, min( 1, text_alpha );
}

if ( text_alpha == 1 )
    fadeSign = -1;

if ( text_alpha == 0 )
    fadeSign = 1;
thank you for the quick reply, this worked perfetly. (except you missed a bracket) ;) but still, much appreciated.
 

Sk8dududu

Member
grr I got another problem that doesn't make sense to me. I want it to flash but with a random phrase.. I have this in my draw event.
Code:
//Draw "Tap to continue"
if draw = 1 && text_alpha > 0
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 1");
}
if draw = 2 && text_alpha > 0
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 2");
}
if draw = 3 && text_alpha > 0
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 3");
}
if draw = 4 && text_alpha > 0
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"tap 4");
}
if draw = 5 && text_alpha > 0
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 5");
}
if draw = 6 && text_alpha > 0
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 6");
}
if draw = 7 {
    draw_set_font(font_intro1)
    draw_set_color(c_white)
    draw_text(x,y,"Tap 7")
}
if draw = 8 {
    draw_set_alpha(text_alpha);
    draw_set_font(font_intro1)
    draw_set_color(c_white)
    draw_text(x,y,"Tap 8")
}
if draw = 9 {
    draw_set_alpha(text_alpha)
    draw_set_font(font_intro1)
    draw_set_color(c_white)
    draw_text(x,y,"Tap 9")
}
and in my alarm event I have
Code:
draw = irandom_range(1,9);
(I already have randomize(); set up in my room creation code to set different seeds, as well the create and step event are the same as you posted above.. it currently isn't drawing anything, but I want it so that it picks a random number from irandom and then what ever number it picks , it displays that line of text with the same fade in and fade out effect that continues forever, or until the object is destroyed..
 
T

TimothyAllen

Guest
Why not just use your old code that was working. Then in your alarm just add:

phrase = choose("phrase 1", "phrase 2", "phrase 3", "phrase 4", );

And use phrase when you are drawing the text.
 

Sk8dududu

Member
like this? :/
Code:
//Draw "Tap to continue"
if phrase1 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 1");
}
if phrase2 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 2");
}
if phrase3 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 3");
}
if phrase4 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"tap 4");
}
if phrase5 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 5");
}
if phrase6 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_color(c_white);
    draw_set_font(font_intro1);
    draw_text(x,y,"Tap 6");
}
if phrase7 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_font(font_intro1);
    draw_set_color(c_white);
    draw_text(x,y,"Tap 7");
}
if phrase8 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_font(font_intro1);
    draw_set_color(c_white);
    draw_text(x,y,"Tap 8");
}
if phrase9 && (text_alpha > 0)
{
    draw_set_alpha(text_alpha);
    draw_set_font(font_intro1);
    draw_set_color(c_white);
    draw_text(x,y,"Tap 9");
}
 

Sk8dududu

Member
Or do I declare the phrase in create? Like
Code:
phrase1="tap1" ;
And so on. And then just use the code from before in the draw event but have
Code:
draw_text(x,y,phrase);
 

sp202

Member
Oh my lord, what a mess. Replace all that code with this:
Code:
text=choose("phrase1","phrase2","phrase3")
draw_set_alpha(text_alpha);
draw_set_font(font_intro1)
draw_set_color(c_white);
draw_text(x,y,text)
As for fading in and out, that alarm method is ridiculous. You can map 0 and 1 onto a sine wave and use that to control the alpha like so:
Code:
text_alpha=0.5*sin(n)+0.5
n+=0.01
 

Sk8dududu

Member
Oh my lord, what a mess. Replace all that code with this:
Code:
text=choose("phrase1","phrase2","phrase3")
draw_set_alpha(text_alpha);
draw_set_font(font_intro1)
draw_set_color(c_white);
draw_text(x,y,text)
As for fading in and out, that alarm method is ridiculous. You can map 0 and 1 onto a sine wave and use that to control the alpha like so:
Code:
text_alpha=0.5*sin(n)+0.5
n+=0.01
lmfao. yeah i realized there was ~70 lines of code . to draw 1 of 10 things. I need the alarm becase I don't want it to draw when the room starts, I want it to start flashing after the alarm activates. 300 steps into the room. I'll try this out.
 
M

Monsi

Guest
As for fading in and out, that alarm method is ridiculous. You can map 0 and 1 onto a sine wave and use that to control the alpha like so:
Code:
text_alpha=0.5*sin(n)+0.5
n+=0.01
Was wondering how long it would be before someone suggested this.

You can also lose the n by using current_time so it's just
Code:
alpha = 0.5*sin(current_time*0.01) + 0.5;
 
M

Monsi

Guest
Clever, but then the sine wave wouldn't start at 0 right? Would look odd if the text went from fully opaque to transparent in an instant.
Yeah no, it depends how you're going to use it I guess. If you can hide it with a transition or use it for debugging it works great. Not having to set up a create event for a simple test is always welcomed!
 
Top