This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dalvengyr
Dalvengyr wrote ...

2014/11/17

Text snippet

Dalvengyr Dalvengyr

2014/11/17

#
I just want to share my "write text" snippet. I hope this snippet can ease you to display texts somewhere. I'm still very new to Greenfoot (javascripting specifically) btw, but I'm used to be active at other coding site and used to write snippets. So let's see if this is acceptable here.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;

/*      ~ Display Text v1.3 ~
 * A simple snippet allows you to display texts
 * in ease.
 * 
 * Check demo at:
 * http://www.greenfoot.org/topics/find/47072#post_47072
 * 
 * Last updates:
 * - Added lifespan and fade out functionality
 * - Added update text and color feature
 */ 

public class Text extends Actor
{
    private int Lifespan = 0;
    private int FadeRate = 0;
    private int Length;
    private int R;
    private int G;
    private int B;
    private int A;
    private int S;
    private String Str;
    
    public void act() 
    {
        if (Lifespan>0)
        {
            Lifespan--;
            if (Lifespan <= 0)
            {
                getWorld().removeObject(this);
                return;
            }
        }
        
        if (FadeRate>0)
        {
            A = A - FadeRate;
            if (A <= 0)
            {
                getWorld().removeObject(this);
                return;
            }
            else
                updateImage(getImage());
        }
    }    
    
    public void write(String s, int size, int red, int green, int blue, int alpha)
    {
        R = red;
        G = green;
        B = blue;
        A = alpha;
        S = size;
        Str = s;
        Length = s.length();
        updateImage(new GreenfootImage((Length*size)*2, size));
    }
    
    public void updateText(String s, int size)
    {
        S = size;
        Str = s;
        updateImage(getImage());
    }
    
    public void updateColor(int red, int green, int blue, int alpha)
    {
        R = red;
        G = green;
        B = blue;
        A = alpha;
        updateImage(getImage());
    }
    
    public void setLifespan(int dur)
    {
        Lifespan = dur;
    }
    
    public void fadeOut(int rate)
    {
        FadeRate = rate;
    }
    
    private void updateImage(GreenfootImage img)
    {
        Color color = new Color(R, G, B, A);
        Font font = img.getFont();
        
        font = font.deriveFont((float)S);
        img.clear();
        img.setFont(font);
        img.setColor(color);
        img.drawString(Str, (Length*S), S);
        setImage(img);
    }
}
How to use:
                Text txt = new Text();
                // Display red "wow" text with 24 font size
                txt.write("wow", 24, 255, 0, 0, 255);
                // Place the text at x: 100, y: 100
                getWorld().addObject(txt, 100,100);
I'm sorry for my poor attempt :( This is how it looks like (yeah, I'm working on a tower defense game): Open here
You need to login to post a reply.