sim salabim
sim salabim

Reputation: 11

How to use a variable in two classes?

I need to be able to see in all of these classes if the variable is true.

    public void performAction() {
    if (door.intersects(HERO)) {
        System.out.println("ActionPerformed!");
        HeroX = 0;
        HeroY = 0;
        inside = true;
        }
    }

This is every time I press SPACE and now I want to draw the inside of the House. In the Main class where I draw everything I want to say something like:

    public void paintComponent(Graphics g) {
    if (!inside) {
        g.drawImage(Background, 0, 0, null);
        achilles.Draw(g);
    }else if (inside) {
        g.drawImage(HouseInside, 0, 0, null);
         }
    }

I don't know how to change the "inside" in the Hero class and use it in the Main class. I have tried so many things and I don't know what to do.

Upvotes: 0

Views: 67

Answers (5)

Thomas Junk
Thomas Junk

Reputation: 5676

You could model it differently:

public class House {

    Location currentLocation=Location.OUTSIDE;

    Map<Location, String> layout=new HashMap<>();

    public House(){
        layout.put(Location.INSIDE,"Draw the inner parts");
        layout.put(Location.INSIDE,"Draw the outer parts");
    }

    public void enterDoor(){
        currentLocation=(currentLocation==Location.OUTSIDE)?Location.INSIDE:Location.OUTSIDE;
    }

    public void draw(){

        System.out.println(layout.get(currentLocation));

    }

}

Here enterDoor is something, which could be fired by an event in your game-environment. So your House knows how to draw itself depending on its state.

Say your character moves to some coordinates (x|y), where the door is, the enterDooris called upon House, which in turn switches the layout.

Upvotes: 0

NinjaBlob
NinjaBlob

Reputation: 111

Try using a getter to get the inside attribute outside of Hero, and a setter to change it:

public class Hero {
    private boolean inside;

    ...        

    public boolean isInside() {
        return this.inside;
    }

    public void setInside(boolean inside) {
        this.inside = inside;
    }
}

I'd also recommend reading more about encapsulation; it'll give you a better idea about how to work with objects in Java.

Upvotes: 0

Trevor Brown
Trevor Brown

Reputation: 169

If you Main class has a reference to all instances of the Hero class, then the usual best practice would be for the Hero class to expose a method like this:

public boolean isInside () {
    return (inside);
}

If there is only ever one instance of the Hero class (unlikely, but ok), then one option is to make inside a public static variable like this:

public static boolean inside;

referenced from Main like this:

if (! Hero.inside) {

or make inside a private static variable with a getter like this:

private static boolean inside;

public static boolean isInside () {
    return (inside);
}

referenced from Main like this:

if (! Hero.isInside ()) {

Upvotes: 0

hamena314
hamena314

Reputation: 3109

What you probably want are global variables

public class Global{
    public static int value;
}

You can then access them from anywhere:

Global.value;

Upvotes: 1

Jeremy
Jeremy

Reputation: 31

inside is a property of a HERO object, so, if the property is public, you can access it with heroname.inside

If the property is private (which it generally should be), you have to use a public access function inside the HERO class, such as HERO.isInside, and set it with a setting function like HERO.setInside and HERO.setOutside.

this is often called "getters and setters"

Upvotes: 1

Related Questions