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

2018/4/15

Danpost explanation?

maboidanpost maboidanpost

2018/4/15

#
Your code worked PERFECTLY, but I feel like I'm not sure about what each line in test() does. Could you give me info on each line, or a summary if that is too much? Thanks. I also added the image list you wanted.
import greenfoot.*;
public class Score extends Actor
{
    GreenfootImage image1;
    GreenfootImage image2;
    GreenfootImage image3;
    GreenfootImage image4;
    GreenfootImage image5;
    GreenfootImage image6;
    GreenfootImage image7;
    GreenfootImage image8;
    GreenfootImage image9;
    GreenfootImage image0;
    public Score(){
        image1= new GreenfootImage("1.png");
        image2= new GreenfootImage("2.png");
        image3= new GreenfootImage("3.png");
        image4= new GreenfootImage("4.png");
        image5= new GreenfootImage("5.png");
        image6= new GreenfootImage("6.png");
        image7= new GreenfootImage("8.png");
        image8= new GreenfootImage("8.png");
        image9= new GreenfootImage("9.png");
        image0= new GreenfootImage("0.png");
    }
    public void act() 
    {
        test();
    }    
    public void test(){
        GreenfootImage[] digitImage= {image1,image2,image3,image4,image5,image6,image7,image8,image9,image0};
        String numberString = ""+Fighter.score;
        int digitCount = numberString.length();
        GreenfootImage[] digitImages = new GreenfootImage[digitCount];
        int textImageWidth = 0;
        for (int i=0; i<digitCount; i++)
        {
            digitImage[i] = new GreenfootImage(""+numberString.charAt(i)+".png");
            textImageWidth += digitImage[i].getWidth();
        }
        GreenfootImage textImage = new GreenfootImage(textImageWidth, digitImage[0].getHeight());
        int drawPosition = 0;
        for (int i=0; i<digitImage.length; i++)
        {
            textImage.drawImage(digitImage[i], drawPosition, 0);
            drawPosition += digitImage[i].getWidth();
        }
        setImage(textImage);
    }
}
danpost danpost

2018/4/16

#
I did not say anything about an image list. Remove lines 4 through 25 and line 31. In the test method, change all digitImage references to digitImages (I corrected that in the other discussion thread). So that the image is not constantly being recreated, do the following:
// add field
private int lastScore;

// constructor
public Score()
{
    test();
}

// act method
public void act()
{
    if (lastScore != Fighter.score)
    {
        test();
        lastScore = Fighter.score;
    }
}
What each line does: line 32 creates a String representation of the value Fighter.score; line 33 gets the length of that String object; line 34 creates an array of that length for the GreenfootImage objects that represent each digit; line 35 initializes a variable used to sum up the width of each image (to get the total width needed for the final image); the first loop (lines 36 through 40) says that for each digit in number (line 36), create a GreenfootImage object for that digit and place it in the array (line 38) and add the width of that image to the variable that sums up the widths (line 39); line 41 creates the final image of appropriate width and height; line 42 initializes a pointer to where the next image in the array is to be drawn on the final image; the second loop (lines 43 through 47) says that for each digit image created (line 43), draw that image on the final image at pointer location (line 45) and adjust the pointer to the next position (line 46); and line 48 sets the final image to the actor.
maboidanpost maboidanpost

2018/4/16

#
My bad, I'm stupid. Thanks for info.
You need to login to post a reply.