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

2021/11/24

int variable does not update in another worlds

miguelcosta miguelcosta

2021/11/24

#
Hi, I'm at the moment making a game for a school project and I got 7 different worlds, in the first world the variable is created and then in the second world, I meantion the first world with "public MyWorld myWorld;" and "myWorld = new MyWorld();" and then update the variables with myworld.(variable) before. But it does not update then. I don't know if I need to update the variable, how should I do it?
miguelcosta miguelcosta

2021/11/24

#
public class MyWorld extends World{
    public int WG=90,WB=90,HB=30,HG=30,FG=50,FB=50;
    public static int pedra = 0;
    public static int madeira = 0;
    private boolean ChangeScene;
    public static int key = 0;
    public Faisca faisca ;
    public Caixa caixa;
    public Caixa1 caixa1;
    public Caixa2 caixa2;
    public void act(){
        showText("Madeira: "+madeira, 625, 755);
        showText("Pedra: "+pedra, 625, 775);
        
        showText("W: " +WB, 64,63);//Sede do Rapaz
        showText("F: " +FB, 65,38);//Comida do Rapaz
        showText("H: " +HB, 65,13);//Vida do Rapaz
        
        showText("W: " +WG, 736,63);//Sede da Rapariga
        showText("F: " +FG, 735,38);//Comida da Rapariga
        showText("H: " +HG, 735,13);//Vida da Rapariga
        vida();
    }
    public MyWorld(){    
        //Cria um mundo 800x800 com resuluçao de 1 pixel
        super(800, 800, 1);
        
        //Indicadores de vida dos jogadores
        //WG=90;WB=90;HB=30;HG=30;FG=50;FB=50; 
        pedra = 0;
        madeira = 0;
        setPaintOrder(Boy.class,Button.class);
        setBackground("world-barco.jpg");
        
        faisca = new Faisca();
        caixa = new Caixa();
        caixa1 = new Caixa1();
        caixa2 = new Caixa2();

        puzzle();
        IndicadoresB();
        IndicadoresG();
            
               
    }
    //Indicadores de vida , fome e sede para o rapaz.
    public void IndicadoresB(){
        addObject(new DropBoy(),15,59);
        addObject(new DropBoy2(),15,59);
        
        addObject(new HealthBoy(),15,15);
        addObject(new HealthBoy2(),15,15);
        
        addObject(new FoodBoy(),15,35);
        addObject(new FoodBoy2(),15,35);
    }
    //Indicadores de vida , fome e sede para a rapariga.
    public void IndicadoresG(){
        addObject(new DropGirl(),785,59);
        addObject(new DropGirl2(),785,59);
        
        addObject(new HealthGirl(),785,15);
        addObject(new HealthGirl2(),785,15);
        
        addObject(new FoodGirl(),785,35);
        addObject(new FoodGirl2(),785,35);
    }
code of my second world
public class Oceano extends World{
    private int counter = 0, counter2 = 0, counter3 = 0;   
    public MyWorld myWorld;
    private int imageCount = 0;
    private int vidaB;
    private int vidaG;
    private int fomeB;
    private int fomeG;
    private int sedeB;
    private int sedeG;
    private GreenfootImage bgImage = new GreenfootImage("oceanoworld.jpg");
    public void act(){
        checkSpawning();
        showText("Madeira: "+myWorld.madeira, 733, 785);
        showText("Pedra: "+myWorld.pedra, 725, 765);
        
        showText("W: " +myWorld.WB, 64,63);//Sede do Rapaz
        showText("F: " +myWorld.FB, 65,38);//Comida do Rapaz
        showText("H: " +myWorld.HB, 65,13);//Vida do Rapaz
        
        showText("W: " +myWorld.WG, 736,63);//Sede da Rapariga
        showText("F: " +myWorld.FG, 735,38);//Comida da Rapariga
        showText("H: " +myWorld.HG, 735,13);//Vida da Rapariga
        
        imageCount -= 1; 
        
        drawBackgroundImage();
    }
    public void drawBackgroundImage(){
        if (imageCount <-bgImage.getWidth()){
            imageCount+= bgImage.getWidth();
        }
        int temp = imageCount;
        getBackground().drawImage(bgImage, temp, 0);
        getBackground().drawImage(bgImage, temp + bgImage.getWidth(), 0);
    }
    public Oceano(){
        super(800, 800, 1); //cria um mundo 800x800 pixels
        //Greenfoot.playSound("som-ambiente.wav"); //musica de fundo
        prepareMundo();
        myWorld = new MyWorld();
        myWorld.IndicadoresB();
        myWorld.IndicadoresG();
    } 
    private void prepareMundo(){
        removeObjects(getObjects(Actor.class)); //limpar o mundo
        
        addObject(new Raft(), 80,330);
        addObject(new Pause(), 25, 775);
        addObject(new TeclaL(), 79, 775);
        addObject(new TeclaS(), 129, 775);
        addObject(new TeclaR(), 179, 775);
        addObject(new TeclaW(), 129, 727);
        
        //Indicadores de vida dos jogadores
        IndicadoresB();
        IndicadoresG();
        
        setPaintOrder(Ilha.class,Raft.class,Boia.class,Banana.class);
    }
danpost danpost

2021/11/25

#
Each world created is given a unique WB, WG, HB, HG, FB and FG fields to work with. The value of one field in one world will not necessarily be equal to the value of the same named field in another world. If you want to retain the value of those fields, you will need to keep a reference to that particular world which contains them. That is, pass that reference to the new world. Either that or put those values in an array and pass the array to the new world when you create it.
miguelcosta miguelcosta

2021/11/25

#
Thanks for awnswering, how would be the best way to pass that reference to the new world? Can you give an example.
danpost danpost

2021/11/25

#
Change line 37 in Oceano class to:
public Oceano(MyWorld mw)
and add at line 39 in Oceano ckass:
myWorld = mw;
If creating the new Oceano object in MyWorld class, use: new Oceano(this) or, if in an Actor subclass with actor in world, use: new Ocean((MyWorld)getWorld())
You need to login to post a reply.