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

2015/4/23

Display number on world

CodeDaySwag CodeDaySwag

2015/4/23

#
i am making a homlessness game and i cant find a easier way to display the amount of money you have left without using a class with a number image so i basically have 1 through 50 classes and all have numbers as images and in my main character i have public void
1
2
3
4
5
6
checkMoney()
if(money == 1))
{
getWorld().addObject(One,
 
}
etc.. FOR. EVERY. NUMBER. is there any easier way to display a number so for example
1
2
3
4
5
6
7
8
9
10
public void CheckMoney()
{
if(money == 1))
{
***DISPLAY NUMBER USING A DIFFERENT WAY THAN ADDING A CLASS***
}
 
 
 
}
thanks in advance and if that the only way then i am okay with that i am pretty sure there is a way easier way to do this
classicjimmy classicjimmy

2015/4/24

#
You would probably want a counter instead of an image. A counter takes a little more effort to implement but you just need one. To create it, use the drawString method in the GreenfootImage package.
1
2
3
4
5
6
7
8
private GreenfootImage image;
 
public void drawImage() {
image = new GreenfootImage(<width>, <height>);
}
public void updateCounter() {
image.drawString(<x>, <y>, money);
}
So you would just create it when the program starts and update it whenever the amount of money changes.
danpost danpost

2015/4/24

#
For a better idea of what you are dealing with, please show the class code for the One class and supply the filenames for the images used for the One, Two and Three classes.
CodeDaySwag CodeDaySwag

2015/4/24

#
I have nothing in the number classes the only reason they are there sso in my player class I have this
1
2
3
4
5
6
7
8
9
Public void check money()
{
If(money == 1))
{
get world().addObject(one, _x_, _y_);
}
 
 
}
Sorry I wrote this whole thing on the phone in hurry so if the code is wrong just ignore it I just put it for the purpose of explaining what I was doing thanks in advance
danpost danpost

2015/4/24

#
Still, what are the image filenames used for the first few classes (one, two, and three)?
CodeDaySwag CodeDaySwag

2015/4/29

#
One.png and Two.png and so on although i dont see why you need that my point is i need to help to display text according to the integer EDIT: i do not have any code in the one, two, three, classes i simply set image
danpost danpost

2015/4/29

#
Well, you could just have one class. The following would be a start (or close to everything you would need) for a Money class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import greenfoot.*;
 
public class Money extends Actor
{
    private static final String[] names = { "Zero", "One", "Two", "Three", "Four", < ... > , "FortyNine", "Fifty" };
 
    private int onHand;
 
    public NumberText()
    {
        setAmountOnHand(onHand);
    }
 
    public void setAmountOnHand(int amount)
    {
        onHand = amount;
        if (onHand > 50) onHand = 50;
        if (onHand < 0) onHand = 0;
        updateImage();
    }
 
    public int getOnHand()
    {
        return onHand;
    }
 
    public void adjustOnHand(int amount)
    {
        setAmountOnHand(onHand+amount);
        updateImage();
    }
 
    private void updateImage()
    {
        setImage(new GreenfootImage(names[onHand]+".png"));
    }
}
Line 3 will have to be completed (there is an omission in the middle of the value listing for the array and the names given will have to be verified for correctness).
You need to login to post a reply.