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

2019/3/12

Struggling with accessing variable from another class & world

Kang Kang

2019/3/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PreyPopulation here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PreyPopulation extends World
{
    int prePop, preMut, preHung, preRep;
    /**
     * Constructor for objects of class PreyPopulation.
     * 
     */
    public PreyPopulation()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prePop = Integer.parseInt(Greenfoot.ask("How many population at the start?"));
        preMut = Integer.parseInt(Greenfoot.ask("How much mutation rate?"));
        preHung = Integer.parseInt(Greenfoot.ask("How much hunger?"));
        preRep = Integer.parseInt(Greenfoot.ask("How much reproduction?"));
    }
    public int getPrePop()
    {
        return prePop;
    }
    public void act()
    {
        if(Greenfoot.isKeyDown("Enter")) Greenfoot.setWorld(new MyWorld());
    }
}
public class MyWorld extends World
{
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    int preyPop, preyMut, preyHung, preyRep;
    int predatorPopulation = 15;
    int plantCounter = 1;
    int gameEnd = 60;
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        preyPop = Integer.parseInt(Greenfoot.ask("How many population at the start?"));
        preyMut = Integer.parseInt(Greenfoot.ask("How much mutation rate?"));
        preyRep = Integer.parseInt(Greenfoot.ask("How much reproduction?"));
        for (int i = 0; i < 100; i++)
        {
            setUp(new Plant());
        }
        for (int i = 0; i < preyPop; i++)
        {
            setUp(new Prey());
        }
        for (int i = 0; i < predatorPopulation; i++)
        {
            setUp(new Predator());
        }
    }
    private void setUp(Actor object)
    {   
        int x = Greenfoot.getRandomNumber(600); 
        int y = Greenfoot.getRandomNumber(400); 
        addObject(object,x,y); 
    }
    public void act()
    {
        plantCheck();
        checkExtinct();
    }
    public void plantCheck()
    {
        plantCounter -= 1;
        if(plantCounter == 0)
        {
            setUp(new Plant());
            plantCounter = 1;
        }
    }
    public void checkExtinct()
    {
        if(getObjects(Predator.class).isEmpty())
        {
            Greenfoot.setWorld(new GameOver());   
        }
        if(getObjects(Prey.class).isEmpty())
        {
            Greenfoot.setWorld(new GameOver());
        }
    }
    public void getPrePop()
    {
        PreyPopulation world = (PreyPopulation)getWorld();
        world.getPrePop();
        
    }
    public int getPreyMut()
    {
        return preyMut;
    }
}
From here, I am trying to access the variable prePop that is declared in PreyPopulation from MyWorld, but when I try what the previous discussion said, I am getting an error that getWorld isn't a method. Is there any way to access variable from the another world??
int receivedMut;
int mutation = Greenfoot.getRandomNumber(getMutation()) + 1;

public int getMutation()
    {
        MyWorld world = (MyWorld) getWorld();
        int receivedMut = world.getPreyMut();
        return receivedMut;
    }
Also from my Prey Class, I am trying to access the variable PreyMut at MyWorld but it says null error... What have I done wrong? I know this kind of discussion exists a lot already but still I couldn't figure it out for my own one, sorry. It would be a great mercy if I can get a help : )
danpost danpost

2019/3/12

#
In this particular case, you could make line 11 in the PreyPopulation class declare static (class) fields. Then you can use the class name to access the field values. However, I am concerned that you have the same fields and asks in your MyWorld class, which is where you really want those fields to be (the asks can be done anywhere, within reason, I guess). You cannot call the getMutation method before the actor is placed into a world (that means not from a field declaration line or from a constructor method). If needed, you can override the addedToWorld(World) method to set the value of the mutation field.
Kang Kang

2019/3/13

#
danpost wrote...
In this particular case, you could make line 11 in the PreyPopulation class declare static (class) fields. Then you can use the class name to access the field values. However, I am concerned that you have the same fields and asks in your MyWorld class, which is where you really want those fields to be (the asks can be done anywhere, within reason, I guess). You cannot call the getMutation method before the actor is placed into a world (that means not from a field declaration line or from a constructor method). If needed, you can override the addedToWorld(World) method to set the value of the mutation field.
public void addedToWorld(World world)
    {
        MyWorld myworld = (MyWorld) getWorld();
        int receivedMut = myworld.getPreyMut();
    }
So I tried something like this but when I run, the game pauses itself every few moments. Have I made a mistake? Btw, seems like the way to declare static int is working perfectly for the world. Thank you!
danpost danpost

2019/3/13

#
Remove "int" from line 4 and your getMutation method should only have the following one line:
return recievedMut;
Kang Kang

2019/3/14

#
danpost wrote...
Remove "int" from line 4 and your getMutation method should only have the following one line:
return recievedMut;
Seems like it worked. Thx a lot : )
You need to login to post a reply.