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

2013/7/19

Make text that is a random number?

joeschmoe joeschmoe

2013/7/19

#
Hey people, I need some help. I want to make a little box with random numbers in it. Here's the code I have for this:
public void ChooseNumber()
    {
        setImage(new GreenfootImage("Greenfoot.getRandomNumber(99)", 25, Color.WHITE, Color.PINK));
    
Right now it says in the box when I compile it: Greenfoot.getRandomNumber(99) instead of the actual number. How can I get the text to display the random number?? Thanks for any help!
Zamoht Zamoht

2013/7/19

#
You have put the code in a string which is why the code is displayed and not the number. This should fix it: setImage(new GreenfootImage("" + Greenfoot.getRandomNumber(99), 25, Color.WHITE, Color.PINK));
joeschmoe joeschmoe

2013/7/19

#
I did that, it's very helpful. But now the numbers are freaking out and changing rapidly when I use 'run'. I want to have the test stay some random number, not changing every act.
joeschmoe joeschmoe

2013/7/19

#
Is there some kind of method I can put in to choose the number once? or any other way?
danpost danpost

2013/7/19

#
If you want your actor to be created with a randomly chosen number than set its image in its constructor (anything put in an actor class act method or a method it calls will be executed over and over again when it is placed into the active world).
joeschmoe joeschmoe

2013/7/26

#
So I need to put the line in the prepare() method of the world? Exactly where should it go within this?
public class BackGround extends World
{
    /**
     * Constructor for objects of class StoryLine.
     * 
     */
    public BackGround()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Numbers numbers1 = new Numbers();
        addObject(numbers1, 100, 100);
        Numbers numbers2 = new Numbers();
        addObject(numbers2, 199, 225);
    }
}
danpost danpost

2013/7/26

#
joeschmoe wrote...
So I need to put the line in the prepare() method of the world?
No. You need to put it in the constructor that creates the object you put in the world. You have a 'ChooseNumber' method that obviously is within an Actor subclass (because you are using the 'setImage' method within it). It should be in the constructor of the class that the 'ChooseNumber' method is in. The constructor of that class (using 'ClassName' as the name of the class) should look like this:
public ClassName()
{
    // statements to construct an object of this class here
}
The code within the constructor usually consists of creating or setting its image(s) and/or variables. EDIT: Unless I am not understanding you correctly, at least. The code you just supplied in your BackGround world class did not look out of sorts. Maybe it would help if you show the entire class that the 'ChooseNumber' method is in.
joeschmoe joeschmoe

2013/7/26

#
Thank you danpost, it works perfectly now.
listiliz listiliz

2015/4/18

#
danpost wrote...
joeschmoe wrote...
So I need to put the line in the prepare() method of the world?
No. You need to put it in the constructor that creates the object you put in the world. You have a 'ChooseNumber' method that obviously is within an Actor subclass (because you are using the 'setImage' method within it). It should be in the constructor of the class that the 'ChooseNumber' method is in. The constructor of that class (using 'ClassName' as the name of the class) should look like this:
public ClassName()
{
    // statements to construct an object of this class here
}
The code within the constructor usually consists of creating or setting its image(s) and/or variables. EDIT: Unless I am not understanding you correctly, at least. The code you just supplied in your BackGround world class did not look out of sorts. Maybe it would help if you show the entire class that the 'ChooseNumber' method is in.
Hello, i've been trying this trick, im writing:
public makanan()
{
makanan hasil1=new makanan();
getWorld().addObject(new makanan(),100,100);
}
but it kept saying "at makanan.<init>(makanan.java:19)" can somebody help me? im sorry for my bad english
danpost danpost

2015/4/18

#
listiliz wrote...
Hello, i've been trying this trick, im writing:
public makanan()
{
makanan hasil1=new makanan();
getWorld().addObject(new makanan(),100,100);
}
but it kept saying "at makanan.<init>(makanan.java:19)" can somebody help me? im sorry for my bad english
First of all, it is not a trick. It is where you build the new object (set its field values and initial image -- mainly). What you have in lines 3 and 4 does not do either of these things as far as the created object is concerned. Those lines should be located outside of the 'makanan' class (probably in your world class without the 'getWorld' call). Also, the object you are assigning to the 'hasil1' field is not the same object that you are trying to place in the world. The repeated 'at makanan.<init>(makanan.java:19)' message is telling you that you have created an infinite loop. The code 'new makanan()' at line 3 in your code above tells the system to start executing from line 1 in your code, where line 3 will be executed again and line 1 will be branched to -- again and again until either an Out of memory or a Stack overflow error occurs. Lesson to be learned: never call a method from within itself. There are exceptions to this rule; but, you must know what you are doing and must ensure that the loop created can be exited from. To be more accurate in the rule: never call a method unconditionally from within itself.
listiliz listiliz

2015/4/18

#
danpost wrote...
listiliz wrote...
Hello, i've been trying this trick, im writing:
public makanan()
{
makanan hasil1=new makanan();
getWorld().addObject(new makanan(),100,100);
}
but it kept saying "at makanan.<init>(makanan.java:19)" can somebody help me? im sorry for my bad english
First of all, it is not a trick. It is where you build the new object (set its field values and initial image -- mainly). What you have in lines 3 and 4 does not do either of these things as far as the created object is concerned. Those lines should be located outside of the 'makanan' class (probably in your world class without the 'getWorld' call). Also, the object you are assigning to the 'hasil1' field is not the same object that you are trying to place in the world. The repeated 'at makanan.<init>(makanan.java:19)' message is telling you that you have created an infinite loop. The code 'new makanan()' at line 3 in your code above tells the system to start executing from line 1 in your code, where line 3 will be executed again and line 1 will be branched to -- again and again until either an Out of memory or a Stack overflow error occurs. Lesson to be learned: never call a method from within itself. There are exceptions to this rule; but, you must know what you are doing and must ensure that the loop created can be exited from. To be more accurate in the rule: never call a method unconditionally from within itself.
ah thanks a lot danpost!
You need to login to post a reply.