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

2015/4/10

Save a variable throughout multiple worlds?

1
2
jackandjim jackandjim

2015/4/10

#
Our Player should have 3 lifes throughout the game. The problem is, we have multiple worlds and the lifes get reset every world... Is there any possibility to save the variable throughout the worlds? I'd be happy to give any information if needed. Thank you in advance, J&J!
danpost danpost

2015/4/10

#
You need to show the code of the class(es) that the int field(s) that holds the number of lives is(are) in. You will probably need to supply the codes for your world class(es) also; and the code of the class that you are changing worlds in.
jackandjim jackandjim

2015/4/10

#
Okay, well the life-variable is created above the constructor of the players class:
private double leben;
It will be set to 3 in the constructor:
leben = 3;
And then changed when the player is touching an enemy:
if (isTouching(Gegner1.class)){
            List<Gegner1> gegner1 = getWorld().getObjects(Gegner1.class);
            int  gX = gegner1.get(0).getGX();
            int  gY = gegner1.get(0).getGY();
            if(this.getX() <= gX && getImage().equals(pa4a) ){
                    if(treffer == 0){
                        g1Leben = g1Leben -1;
                    }
                    treffer = 1;
            }
            if(this.getX() >= gX && getImage().equals(pa3a) ){
                    if(treffer == 0){
                        g1Leben = g1Leben -1;
                    }
                    treffer = 1;
            }
            if(this.getY() >= gY && getImage().equals(pa1a) ){
                    if(treffer == 0){
                        g1Leben = g1Leben -1;
                    }
                    treffer = 1;
            }
            if(this.getY() + 50<= gY && getImage().equals(pa2a) ){
                    if(treffer == 0){
                        g1Leben = g1Leben -1;
                    }
                    treffer = 1;
            }
        }
Now we made a labyrinth out of 5x5 pieces of 800x800 px big worlds and everytime you pass a certain X or Y the next world is created and the life-variable resets to 3.... I'd be happy to give any more information if needed.
danpost danpost

2015/4/10

#
First issue to deal with is that you have one name for the lives field 'leben' and are changing a different field when touching an enemy, 'g1Leben'. So, 'leben', as far as I can tell, is never changed. Another thing, while we are dealing with the 'leben' field -- why is it a 'double' field. If it is set to '3' and supposed to decrease by ones, would not an 'int' field be sufficient?
jackandjim jackandjim

2015/4/10

#
Oops, sent you the wrong variable .-. "leben" was the variable of the player ^^
public int g1Leben;
AND
g1Leben = 2;
My fault, apologize.
danpost danpost

2015/4/10

#
Okay. Being it is a 'public' field and you are probably creating a new PlayerChar object in the different worlds, you probably want to just set the 'g1leben' field of the new PlayerChar in the new world to the value of the one in the old (current) world. Show the code of the method (or class) where you create the new world that the 'g1leben' field is reset in (indicate which class the code is in if not providing the entire class code).
jackandjim jackandjim

2015/4/10

#
Oh fuck man, I'm so sorry... Forget everything I sent you, the actual code would be: Okay, well the life-variable is created above the constructor of the players class:
private double leben;
It will be set to 3 in the constructor:
leben = 3;
And then changed when the player is touching an enemy:
private boolean testeAufKollisionUndReagiere(Class GegnerNR){
        boolean esGibtKollision=kollidiertMitObjektVon(GegnerNR);
            if(esGibtKollision == true){
                if(zustand == 1){
                zustand = 2;
                leben = leben-1;
                rausschieben();
                if (leben == 0){
                    Greenfoot.setWorld(new GameOver());
                }
            }else if(zustand == 2){
                rausschieben();
            }
        }
        return esGibtKollision;
    }
danpost danpost

2015/4/10

#
The only world that is gone to through the code given is the GameOver world -- and you know the value of the 'leben' field when it is gone to. So, where are you creating a world that you need the value of the 'leben' field not to be reset?
jackandjim jackandjim

2015/4/10

#
For example:
if(getWorld() instanceof Level1punkt3punkt1 || getWorld() instanceof Level1punkt3punkt2 || getWorld() instanceof Level1punkt3punkt3){
                if(this.getX() <= 15){
                    Greenfoot.setWorld(new Level1punkt4punkt1());
                }
            }
And the world code:
import greenfoot.*;  

public class Level1punkt4punkt1 extends Level
{

    public Level1punkt4punkt1()
    {    
        prepare();
    }
    
    public void prepare(){
        PatAmer patamer = new PatAmer();
        addObject(patamer, 400, 400);
        patamer.setLocation(765, 400);
        Dunkelheit dunkelheit = new Dunkelheit();
        addObject(dunkelheit, 367, 425);
        dunkelheit.setLocation(400, 400);
    }
}
danpost danpost

2015/4/10

#
And your Level class? also, what is the class name for the player (is it PatAmer)?
jackandjim jackandjim

2015/4/11

#
Level Class: We really only created the enemies in this Class.
private int GPL;
    public Level()
    {    
       
        super(800, 800, 1);
        if(this instanceof Level1punkt17punkt1 == false){
            GPL = Greenfoot.getRandomNumber(3) + 2;
            if(GPL == 2 || GPL == 3 || GPL == 4){
                //Gegner1
                Gegner1 gegner1 = new Gegner1();
                addObject(gegner1, 626, 145);
                gegner1.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
                //Gegner2
                Gegner2 gegner2 = new Gegner2();
                addObject(gegner2, 626, 145);
                gegner2.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            }
            if(GPL == 3 || GPL == 4){
                //Gegner3
                Gegner3 gegner3 = new Gegner3();
                addObject(gegner3, 626, 145);
                gegner3.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            }
            if(GPL == 4){
                //Gegner4
                Gegner4 gegner4 = new Gegner4();
                addObject(gegner4, 626, 145);
                gegner4.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            }
        }else{
            //Gegner1
            Gegner1 gegner1 = new Gegner1();
            addObject(gegner1, 626, 145);
            gegner1.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            //Gegner2
            Gegner2 gegner2 = new Gegner2();
            addObject(gegner2, 626, 145);
            gegner2.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            //Gegner3
            Gegner3 gegner3 = new Gegner3();
            addObject(gegner3, 626, 145);
            gegner3.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            //Gegner4
            Gegner4 gegner4 = new Gegner4();
            addObject(gegner4, 626, 145);
            gegner4.setLocation(Greenfoot.getRandomNumber(600) + 100,Greenfoot.getRandomNumber(350) + 250);
            //Endgegner
            Endgegner1 endgegner1 = new Endgegner1();
            addObject(endgegner1, 626, 145);
            endgegner1.setLocation(400,400);
        }
    }
}
And yes, PatAmer is the player.
danpost danpost

2015/4/11

#
Instead of just setting a new world active with this:
Greenfoot.setWorld(new Level1punkt4punkt1());
create a local variable to reference the new world, like this:
World level141 = new Level1punkt4punkt1();
PatAmer pa141 = (PatAmer)level141.getObjects(PatAmer.class).get(0);
pa141.g1leben = g1leben;
Greenfoot.setWorld(level141);
and change 'private double g1leben;' to 'public int g1leben;'.
jackandjim jackandjim

2015/4/12

#
I set g1Leben to public:
public int g1Leben;
And then used your code:
if(getWorld() instanceof Level1punkt1punkt1 || getWorld() instanceof Level1punkt1punkt2){
                if( this.getY() <= 15){
                    World level121 = new Level1punkt2punkt1();
                    PatAmer pa121 = (PatAmer)level121.getObjects(PatAmer.class).get(0);
                    pa121.g1Leben = g1Leben;
                    Greenfoot.setWorld(level121);
                }
            }
It doesn't give us an error but the lifes still reset to 3 when the world changes.
danpost danpost

2015/4/12

#
Maybe the value never changes from three to begin with. Try this and report back:
if(getWorld() instanceof Level1punkt1punkt1 || getWorld() instanceof Level1punkt1punkt2){
    if( this.getY() <= 15){
        World level121 = new Level1punkt2punkt1();
        PatAmer pa121 = (PatAmer)level121.getObjects(PatAmer.class).get(0);
        System.out.println("Value of 'pa121.g1Leben':   "+pa121.g1Leben);
        pa121.g1Leben = g1Leben;
        Greenfoot.setWorld(level121);
    }
}
jackandjim jackandjim

2015/4/12

#
The value does reset after world change...
Value of 'pa121Leben':  3
if(getWorld() instanceof Level1punkt1punkt1 || getWorld() instanceof Level1punkt1punkt2){
                if( this.getY() <= 15){
                    World level121 = new Level1punkt2punkt1();
                    PatAmer pa121 = (PatAmer)level121.getObjects(PatAmer.class).get(0);
                    System.out.println("Value of 'pa121Leben*':  "+pa121.leben);
                    pa121.leben = leben;
                    Greenfoot.setWorld(level121);
                }
            }
There are more replies on the next page.
1
2