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

2018/2/5

Accessing a variable from a different class

Acituanbus Acituanbus

2018/2/5

#
I've seached for how to do this before, but none of the things I tried worked. So what im trying to do is change the startlocation of the player whenever he exits a house, so that once the "outside" map (called environment) is generated, it doesnt start in the middle of the map. This is in the Actor-class "Variables":
import greenfoot.*;  
public class Variables extends Actor
{
    private int startlocation = 0;
    public void startlocation(int startlocation){this.startlocation=startlocation;}
    public int getstartlocation(){return startlocation;}
}
This is the code that generates the world and is supposed to change the variable:
import greenfoot.*;  
public class Player extends Actor
{
public void doors(){
        Actor ondoor3 = getOneIntersectingObject(Door3.class);
        if (ondoor3!=null){
            getWorld().removeObject(ondoor3);
            Greenfoot.delay(10);
            Environment environment = new Environment();
            Greenfoot.setWorld(environment);
            startlocation(1);
        }
    }
    public void act(){
        doors();
    }
}
And this is the "environment" class, in which Im trying to set the players location:
import greenfoot.*; 
public class Environment extends World
{
    public Environment()
    {    
        super(1280, 720, 1);
        prepare();
        
        overlays();
        
        Player player = new Player();
        
        if(player.getstartlocation()==0){addObject(player, 700,300);}
        if(player.getstartlocation()==1){addObject(player, 640,509); player.setRotation(-90);}
    }
}
Hope some of you have an idea on how to do this if you need anything else just let me know Thanks :D
danpost danpost

2018/2/5

#
Line 11 of the Player class calls a method in a non-associated class. The Variable class does not appear actually be an actor in your project. Having that class extend the Actor class does not make any sense. Also, I do not see anywhere where you are creating any Variable objects. I would suggest you delete the Variable class and move lines 4 through 6 from within it to the Player class as static content (add the 'static' modifier to those 3 lines. Then adjust the 'startLocation' value before creating a new Environment object. You can just use the class name to access the two methods -- you do not need a Player object.
Acituanbus Acituanbus

2018/2/5

#
Okay, I removed the variables class and put the 3 lines into player, but for some reason its still not working:
import greenfoot.*; 
    public class Player extends Actor
    {
    private static int startlocation = 0;
    public static void startlocation(int startlocation){startlocation=startlocation;}
    public static int getstartlocation(){return startlocation;}

    public void doors(){
        Actor ondoor3 = getOneIntersectingObject(Door3.class);
        if (ondoor3!=null){
            getWorld().removeObject(ondoor3);
            Greenfoot.delay(10);
            startlocation(1);
            Environment environment = new Environment();
            Greenfoot.setWorld(environment);
        }

    public void act(){
        doors();
    }
}
This is the environment class:
    import greenfoot.*;
public class Environment extends World
{
    public Environment()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1280, 720, 1);
        prepare();
        
        overlays();
        
        Player player = new Player();
        
        if(player.getstartlocation()==0){addObject(player, 700,300);}
        if(player.getstartlocation()==1){addObject(player, 640,509); player.setRotation(-90);}
    }
}

It still places the player at 700 - 300 though. Any idea why? Thanks for the help btw
Super_Hippo Super_Hippo

2018/2/5

#
Line 5 in the Player class can't work as it is. You either need to use 'this.startlocation = startlocation' or you change the name of the method's parameter. Then you can also give it a better name:
public static void setStartlocation(int newLocation) {startlocation = newLocation;}
Acituanbus Acituanbus

2018/2/5

#
Thanks!
You need to login to post a reply.