Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 3:06 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Image and String fades
PostPosted: Fri Feb 17, 2012 2:00 pm 
Offline
User avatar

Joined: Sat Apr 30, 2011 6:18 am
Posts: 21
Hi all,

How do i have Strings and Images fade in and out?

Thank you!

_________________
You can never finish a program, only put it down.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 5:55 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
I can't test it right now but try this:

Code:
time+=delta;
while (time > 100)
       {
       someImage.setAlpha(someImage.getAlpha + 0.1f);
       time -= 100;
       }


What this does is add 0.1f to the alpha (I'm guessing it's 0 is invisible 1 is completely visible) of the image. I'm really not sure how you'd do strings.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 11:15 pm 
Offline
User avatar

Joined: Sat Apr 30, 2011 6:18 am
Posts: 21
Thank you!

This worked a treat. All i had to add was setting the image Alpha to 0.

Code:
private Image someImage;

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
      
      someImage = new Image("img/test.png");
      someImage.setAlpha(0);
}


Now, does anyone know how to perform a similar application with Strings?

Thank you.

_________________
You can never finish a program, only put it down.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 11:34 pm 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
It very similar to how you do with images.

If you are using the graphics to drawstring, simply set the color of the graphics object just before drawing.

if you are using a font to draw, use this method.
Code:
font.drawString(x ,y ,"" , color);

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 12:31 am 
Offline
User avatar

Joined: Sat Apr 30, 2011 6:18 am
Posts: 21
I have tried applying this method but i must be missing something. This just displays text to the screen instantly.

Code:

Color fadeColor;
Double fadeTime;

init{
fadeColor = new Color((1.0f, 1.0f, 1.0f, 0.0f);
}

render{
g.setColor(fade);
g.drawString("fading text", 400, 300);
}

update{
fadeTime += delta;
while (fadeTime > 100) {
   fade.a = (fade.getAlpha() + 0.01f);
   fadeTime -= 100;
}
}



Thanks heaps.

_________________
You can never finish a program, only put it down.


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 2:08 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
This worked for me at a time i needed to create a cursor for a touch screen, that faded out when the screen were no longer being touched.
It wasn't done in Slick 2D but Java 2D, but it seems to work fine here too.
Code:
   private int timer;
   private int timerLast = 1500;
   boolean fade;

   public void update(GameContainer gc, int delta) throws SlickException {
      if (fade) {
         timer += delta;
         if (timer > timerLast) {
            fade = !fade;
         }
      } else {
         timer -= delta;
         if (timer < 0) {
            fade = !fade;
         }
      }
   }
   
   public void render(GameContainer gc, Graphics g) throws SlickException {
      float timerPercent = (float) timer / timerLast;
      int alphaPercent = (int) (255 * timerPercent);
      g.setColor(new Color(255, 255, 255, alphaPercent));
      g.drawString("test", 20, 30);
   }


Edit: added code to illustrate fading in and out.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Sun Feb 19, 2012 11:06 am 
Offline
User avatar

Joined: Sat Apr 30, 2011 6:18 am
Posts: 21
Thank you sir, this worked a charm.

_________________
You can never finish a program, only put it down.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group