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

2013/11/3

Health in pictures

Tioma7 Tioma7

2013/11/3

#
Im wondering is there a way to make health shown by pictures in one object. public void HealthSet(){ int x = Player.Health % 10; switch (x){ case 0: setImage("0.png"); break; case 1: setImage("1.png"); break; case 2: setImage("2.png"); break; case 3: setImage("3.png"); break; case 4: setImage("4.png"); break; case 5: setImage("5.png"); break; case 6: setImage("6.png"); break; case 7: setImage("7.png"); break; case 8: setImage("8.png"); break; case 9: setImage("9.png"); break; } I wrote this but it shows 1 digit of health i one object, is there a way to make 3 digits in one object?
danpost danpost

2013/11/3

#
Tioma7 wrote...
I wrote this but it shows 1 digit of health i one object, is there a way to make 3 digits in one object?
Yes. It would require drawing the images of the digits side-by-side on an image large enough to hold all the images.
Zamoht Zamoht

2013/11/3

#
If you want to display health you could also take a look at the HealthBar class from my Animated Health Bar scenario.
Tioma7 Tioma7

2013/11/3

#
danpost wrote...
Tioma7 wrote...
I wrote this but it shows 1 digit of health i one object, is there a way to make 3 digits in one object?
Yes. It would require drawing the images of the digits side-by-side on an image large enough to hold all the images.
Can you tell me how to do that because im new to greenfoot and dont know all the codes yet
danpost danpost

2013/11/3

#
You would need to create GreenfootImage objects of the digits you need, sum up the widths of those images and create a new GreenfootImage that is as wide as the sum and as high as any one of the digit images. Then, use 'drawImage' to draw the digit images onto the wide image. You will have to track the sum of the images drawn to determine where the next image should be drawn at. Finally, set the image of the actor to the wide image.
Tioma7 Tioma7

2013/11/3

#
danpost wrote...
You would need to create GreenfootImage objects of the digits you need, sum up the widths of those images and create a new GreenfootImage that is as wide as the sum and as high as any one of the digit images. Then, use 'drawImage' to draw the digit images onto the wide image. You will have to track the sum of the images drawn to determine where the next image should be drawn at. Finally, set the image of the actor to the wide image.
i think i have problem with this: public class Health extends Actor { GreenfootImage Digit1 = new GreenfootImage("0.pg"); GreenfootImage Digit2 = new GreenfootImage("0.pg"); GreenfootImage Digit3 = new GreenfootImage("0.pg"); GreenfootImage Health; public void act() { Health.drawImage(Digit1 + Digit2 + Digit3,36,12); } public void asd(){ } }
danpost danpost

2013/11/3

#
You have not created an image for 'Health'. You cannot use '+' to draw multiple images. Rename the 'Health' field in the 'Health' class. Convention has field names beginning with lowercase letter and class names beginning with uppercase.
Tioma7 Tioma7

2013/11/3

#
danpost wrote...
You have not created an image for 'Health'. You cannot use '+' to draw multiple images. Rename the 'Health' field in the 'Health' class. Convention has field names beginning with lowercase letter and class names beginning with uppercase.
what metode i need to use to draw multiple image in one?
danpost danpost

2013/11/3

#
You need to use the GreenfootImage 'drawImage' method multiple times (once for each digit).
Tioma7 Tioma7

2013/11/3

#
danpost wrote...
You need to use the GreenfootImage 'drawImage' method multiple times (once for each digit).
But it will repaint my Health image every digit and i will get one, isnt it?
danpost danpost

2013/11/3

#
Not if you draw all the digits onto one larger GreenfootImage object; something like (but not exactly) this:
GreenfootImage image = new GreenfootImage(48, 32);
image.drawImage(firstDigitImg, 0, 0);
image.drawImage(secondDigitImg, 16, 0);
image.drawImage(thirdDigitImg, 32, 0);
setImage(image);
This just shows you what I mean by drawing multiple digit images on one larger image. You still would have to determine how many digit images need to be drawn, the correct size to make the larger image and where each image needs to be drawn.
Tioma7 Tioma7

2013/11/3

#
danpost wrote...
Not if you draw all the digits onto one larger GreenfootImage object; something like (but not exactly) this:
GreenfootImage image = new GreenfootImage(48, 32);
image.drawImage(firstDigitImg, 0, 0);
image.drawImage(secondDigitImg, 16, 0);
image.drawImage(thirdDigitImg, 32, 0);
setImage(image);
This just shows you what I mean by drawing multiple digit images on one larger image. You still would have to determine how many digit images need to be drawn, the correct size to make the larger image and where each image needs to be drawn.
Now i got this thx a lot!!!
Tioma7 Tioma7

2013/11/4

#
Anyone could tell where is my mistake because it doesnt draw: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Health extends Actor { private int digit1 = Player.health / 100; private int digit2 = Player.health / 10 % 10; private int digit3 = Player.health % 10; private int Delay = 0; public void act() { draw(); } public void draw(){ digit1 = Player.health / 100; digit2 = Player.health / 10 % 10; digit3 = Player.health % 10; GreenfootImage Digit1 = new GreenfootImage(12,12); GreenfootImage Digit2 = new GreenfootImage(12,12); GreenfootImage Digit3 = new GreenfootImage(12,12); getDigit(Digit1,digit1); getDigit(Digit2,digit2); getDigit(Digit3,digit3); GreenfootImage Health = new GreenfootImage(36,12); Health.drawImage(Digit1,0,0); Health.drawImage(Digit2,12,0); Health.drawImage(Digit3,24,0); setImage(Health); } public void getDigit(GreenfootImage a,int b){ switch(b){ case 0 : a = new GreenfootImage("Digits/0.png"); break; case 1 : a = new GreenfootImage("Digits/1.png"); break; case 2 : a = new GreenfootImage("Digits/2.png"); break; case 3 : a = new GreenfootImage("Digits/3.png"); break; case 4 : a = new GreenfootImage("Digits/4.png"); break; case 5 : a = new GreenfootImage("Digits/5.png"); break; case 6 : a = new GreenfootImage("Digits/6.png"); break; case 7 : a = new GreenfootImage("Digits/7.png"); break; case 8 : a = new GreenfootImage("Digits/8.png"); break; case 9 : a = new GreenfootImage("Digits/9.png"); break; default: a = new GreenfootImage("Digits/0.png"); break; } } }
danpost danpost

2013/11/4

#
The main problem that I see is in your 'getDigit' method. You have it receiving an image and a single-digit int value. The method sets the image to a new image, which removes any image you previously gave it, and then the method is exited, which loses the image change altogether. I do not think you realize that the image received by the method becomes local to the method and no longer is linked in any way to the image created by the calling method (the field itself is not passed to the method, just its value). Therefore, you will need to pass the new image back to the calling method by changing its return type from 'void' to 'GreenfootImage' and adding a 'return' statement at the end of the method that returns the new GreenfootImage object. In the calling statement, you need to 'catch' the returned value into a GreenfootImage field. The following is just an example of what is necessary:
// the calling statement
GreenfootImage digitImg1 = getDigitImg(b);
// the method
public GreenfootImage getDigitImg(int b)
{
    GreenfootImage digitImg = null;
    switch(b)
    {
        // cases setting digitImg to appropriate image
    }
    return digitImg;
}
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
The main problem that I see is in your 'getDigit' method. You have it receiving an image and a single-digit int value. The method sets the image to a new image, which removes any image you previously gave it, and then the method is exited, which loses the image change altogether. I do not think you realize that the image received by the method becomes local to the method and no longer is linked in any way to the image created by the calling method (the field itself is not passed to the method, just its value). Therefore, you will need to pass the new image back to the calling method by changing its return type from 'void' to 'GreenfootImage' and adding a 'return' statement at the end of the method that returns the new GreenfootImage object. In the calling statement, you need to 'catch' the returned value into a GreenfootImage field. The following is just an example of what is necessary:
// the calling statement
GreenfootImage digitImg1 = getDigitImg(b);
// the method
public GreenfootImage getDigitImg(int b)
{
    GreenfootImage digitImg = null;
    switch(b)
    {
        // cases setting digitImg to appropriate image
    }
    return digitImg;
}
It works thank you!!!
You need to login to post a reply.