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

2013/4/26

help with coding this pls

gusbus123 gusbus123

2013/4/26

#
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).
gusbus123 gusbus123

2013/4/26

#
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.
gusbus123 gusbus123

2013/4/26

#
pls help
You need to login to post a reply.