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

2016/8/7

Using a non-standard character set

davemib123 davemib123

2016/8/7

#
Hi, I am trying to use a custom font , I have created graphics for the numbers 0 - 9 and want to use these to display score value instead of greenfoot text. It sort of works with this:
import greenfoot.*;  

public class Score extends Actor  
{  
    private static final GreenfootImage No1 = new GreenfootImage("1.png");
    
    public Score()  
    {  
        GreenfootImage newImage = new GreenfootImage (100, 50);
        setImage(newImage);
    }  
    
    public void setScore(int score)
    {
       GreenfootImage newImage = getImage();
       newImage.clear();
       if (score == 1)setImage(No1);
    }
}
My question is how do I do it for every possible number? For example if the player gets a score of 999, surely I dont have to make all 999 images? I could, but is there a better way?
danpost danpost

2016/8/7

#
davemib123 wrote...
I am trying to use a custom font , I have created graphics for the numbers 0 - 9 and want to use these to display score value instead of greenfoot text. It sort of works with this: < Code Omitted > My question is how do I do it for every possible number? For example if the player gets a score of 999, surely I dont have to make all 999 images? I could, but is there a better way?
Here are two better ways: (1) just use the digits images (and possibly a 'space' image') and draw the required combination of digits on the image that is to display the value; (2) use my TextImage Support Class in your project to create the score image;
davemib123 davemib123

2016/8/7

#
danpost wrote...
Here are two better ways: (1) just use the digits images (and possibly a 'space' image') and draw the required combination of digits on the image that is to display the value; (2) use my TextImage Support Class in your project to create the score image;
Something like this?
if (score == 123){

drawImage(no1, 0, 0);
drawImage(no2, 25, 0);
drawImage(no3, 50, 0);
}
danpost danpost

2016/8/8

#
davemib123 wrote...
Something like this? < Code Omitted >
No -- that would be akin to creating all 1000 images (except you would be coding for each 1000 values). More like this:
newImage.clear();
int value = score;
newImage.drawImage(digits[value%10], 50, 0);
value = value/10;
int i = 1;
while (value > 0)
{
    newImage.drawImage(digits[value%10], 50-25*i, 0);
    i++;
    value = value/10;
}
where you put all ten digit images in an array:
GreenfootImage[] digits = { no0, no1, no2,, no3, no4, no5, no6, no7, no8, no9 };
You need to login to post a reply.