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

2015/11/5

Problem with Greenfoot.getRandomNumber();

onny_07 onny_07

2015/11/5

#
I have made random number which is stored in a public variable. I want to acces it to the other class. When i get that variable, the number in the other class returns different value. How can i get the same value, eventhough i call the variable through other class ? Thank you.
danpost danpost

2015/11/5

#
onny_07 wrote...
I have made random number which is stored in a public variable.
Show code declaring and setting this field. Also, what class is it declared in and what type of class is it (subclass of World, Actor, or just Object)?
When i get that variable
How are you trying to get the variable (or value of it)? Show code. Cannot fix code not shown. Use code tags when posting code ( refer to the link below the reply box -- 'Posting code? read this! ).
onny_07 onny_07

2015/11/5

#
OK, i'm sorry for that. Here's my code.
public class Actor1 extends Actor
{
    public int number =Greenfoot.getRandomNumber(10);
    
    public Actor1(){
        setImage(new GreenfootImage(""+number, 30, Color.RED, Color.YELLOW));
    }
    /**
     * Act - do whatever the Actor1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
}
I stored random number in "number" variable, and i want the value of number is same when i put in the other Actor class. Like this.
public class Actor2 extends Actor
{
    Actor1 a1 = new Actor1();
    
    public Actor2(){
        setImage(new GreenfootImage(""+a1.number, 30, Color.RED, Color.YELLOW));
    }
    /**
     * Act - do whatever the Actor2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
How can i set "number" in Actor2 equal with "number" in Actor1 ? Thank you.
danpost danpost

2015/11/5

#
I think the problem is that you already have an Actor1 object in the world when you create the Actor2 object and the new Actor2 object creates another Actor1 object which may have a different value for 'number'. If you want the value of 'number' from the Actor1 object already in the world, then either the world class should pass the value or the Actor2 object must wait until it is placed into the world before acquiring that value.
onny_07 onny_07

2015/11/6

#
It still doesn't work. Is there any other way to fix this ?
danpost danpost

2015/11/6

#
Post your World subclass code. I still think that the Actor1 object in the world is not the Actor1 object you are trying to get the value of 'number' from.
onny_07 onny_07

2015/11/6

#
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // 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()
    {
        Actor1 actor1 = new Actor1();
        addObject(actor1, 131, 73);
        Actor2 actor2 = new Actor2();
        addObject(actor2, 245, 143);
    }
}
danpost danpost

2015/11/6

#
Try inserting the following at line 25::
actor2.a1 = actor1;
That should set it to the proper Actor1 object.
onny_07 onny_07

2015/11/6

#
I've tried that, but it still doesn't work.
onny_07 onny_07

2015/11/6

#
I think the problem is in 'number' variable. When i try to access a1.number, what I actually accessed is 'Greenfoot.getRandomNumber(10)'. So I get the 'number' variable with new random number, not the current 'number' of Actor1 class.
danpost danpost

2015/11/6

#
onny_07 wrote...
I've tried that, but it still doesn't work.
Oh, sorry. That is only half the solution because you are setting the image before the value of 'a1' is changed. We need to remove the 'setImage' line from the Actor2 class constructor and add the following line after the inserted line above:
actor2.setImage(new GreenfootImage(""+actor1.number, 30, Color.RED, Color.YELLOW));
Also, you should probably declare 'a1' in the Actor2 class setting it to 'null' instead of to an Actor1 object you will not be using.
danpost danpost

2015/11/6

#
onny_07 wrote...
I think the problem is in 'number' variable. When i try to access a1.number, what I actually accessed is 'Greenfoot.getRandomNumber(10)'. So I get the 'number' variable with new random number, not the current 'number' of Actor1 class.
Accessing the value does not change what it is originally set to.
onny_07 onny_07

2015/11/6

#
Finally, it works. Thank you so much for helping me. I really appreciate it. Keep helping me, if I get stuck next time. :D
You need to login to post a reply.