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

2013/4/27

using sprites to draw strings

gusbus123 gusbus123

2013/4/27

#
i have a sprite sheet called "words sprites.png" and it has the images of all numbers (0-9) next to each other. In my program i have a int called score which holds my current score, it begins at 0. I have a class called Others which i am trying to use to display the score by using the images on the sprite sheet. This needs to work even if the score is bigger than 1 digit, so if the score = 1290 the Others class's image will display 1290 using the images in my sprite sheet. how do i do this? pls provide an example code :) the sheet displays 0-9 in a row with each digit being the size of 7x7 pixels. Each digit in the sprite sheet has a 1 pixel spacing in between (included in the 7x7 box). this is the closest iv got.
private GreenfootImage sprite=new GreenfootImage("words sprites.png");  
    private GreenfootImage image;  
        SpaceInvadersWorld world = (SpaceInvadersWorld) getWorld();  
        String score = world.score+"";  
        GreenfootImage fullWord = new GreenfootImage(score.length()*7,7);  
        for(int i=0; i<score.length(); i++) {  
            for(int a=0; a<10; a++) {  
                int num = score.charAt(i);  
                if(num==a) {  
                    image.clear();  
                    image.scale(image.getWidth()/3,image.getHeight()/3);  
                    imageX=a*-7-1;  
                    image.drawImage(sprite,imageX,imageY);  
                    image.drawImage(fullWord,i*-7,-1);  
                }  
            }  
its supposed to convert the int to a String, then using the for loop it goes through each char in the String and for each of those it will check which number it is using another for loop and depending on what it comes out with it will change the image to the required picture and draw it on top of the fullWord image, and then continue on checking the next number in the string. But i have done something wrong and i cant find it.
danpost danpost

2013/4/27

#
If the "words sprites.png" image contains just the numbers zero through nine (in that order) and each number on the image has the same width, then it would not be too difficult to create some code to do what you want. You first, however, need to draw the sprite image on a single number-image sized image with the appropriate offset to get the correct number on it. Then draw that number-image sized image on the fullWord image at the appropriate offset. The first step effectively creates the image of a single digit. The second puts that digit on the fullWorld image. It might be easiest to start with the last right-most digit and work left. You would be able to determine each digit from right to left by getting the remainder when dividing by 10; then divide it by 10 and repeat for the next digit. 'Do' this 'while' the score is greater than zero. Track the number of digits to keep track of where to draw each digit on the 'fullWord' image.
gusbus123 gusbus123

2013/4/27

#
that is what iv been trying to do, but iv been having big troubles trying to do it
danpost danpost

2013/4/27

#
This is something close to what I was trying to say:
private GreenfootImage sprite=new GreenfootImage("words sprites.png");  
int score = ((SpaceInvadersWorld) getWorld()).score;
GreenfootImage fullWord = new GreenfootImage(/* max digits*7 */, 7);
int digitNum = /* max digits */;
do
{
    int digit = score % 10;
    score = score / 10;
    GreenfootImage digit = new GreenfootImage(7, 7);
    digit.drawImage(sprite, -7*digit, 0);
    fullWord.drawImage(digit, digitNum*7, 0);
    digitNum--;
} while (score > 0);
gusbus123 gusbus123

2013/4/27

#
ty, this helped alot
You need to login to post a reply.