Search code examples
javaclassvariables

Java one variable in two classes?


I tried to use a variable in two classes in Java because I'm currently programming a game that requires this.

For example, my issue:

Here's my specific problem: I have a Rocket and a Meteor defined as separate classes. The Rocket either avoids the Meteor or shoots it down with a 'Bullet'. The player earns a few points for every second survived in a score counter. The counter is set as a variable in the Rocket class so that the variable only increases when the Rocket is active. However, the player should also earn extra points for shooting down a Meteor. Since the counter does not exist in the Meteor class, even after extensive searching, I don't know how to increase my counter in the Meteor class as well.

I programmed this in Greenfoot for my actual task that I was given. This represents my Rocketclass and should show you how my script is structured. I hope you can infer something from it.

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

    public class Rocket extends Actor
    {
    private int zaehler_02 =0;//wait for shoot
    private int zaehler_03 =0;//addbullet
    private int zaehler_05 =0;//counter
    private int magazin = 25;`
    
    public void act() 
    {
        if(Greenfoot.isKeyDown("right")){
            this.MoveRight();
        }
        if(Greenfoot.isKeyDown("left")){
            this.MoveLeft();
        }
        if(Greenfoot.isKeyDown("up")){
            this.MoveUp();
        }
        if(Greenfoot.isKeyDown("down")){
            this.MoveDown();
        }`
        
       `zaehler_03 += 1;
        zaehler_02 += 1;
        zaehler_05 += 1;
        if(Greenfoot.isKeyDown("space")){
            if(zaehler_02 >=35 && magazin >= 1){
                this.bullet();
                zaehler_02 = 0;
                magazin += -1;
            }
        }
        
        this.counter();
        this.getBullets();
        this.checkcollision();
    }    

    public void MoveRight(){
        this.setLocation(this.getX()+3, this.getY());
    }

    public void MoveLeft(){
        this.setLocation(this.getX()-3, this.getY());
    }

    public void MoveUp(){
        this.setLocation(this.getX(), this.getY()-3);
    }

    public void MoveDown(){
        this.setLocation(this.getX(), this.getY()+3);
    }
    
    public void bullet(){
        Bullet bullet = new Bullet();
        this.getWorld().addObject(bullet, this.getX(),this.getY());
    }
    
    public void counter(){
        count = zaehler_05;
        count /= 20;
        this.getWorld().showText("Score:" + Integer.toString(count), 60, 20);
        this.getWorld().showText("Magazin:" + Integer.toString(magazin), 200, 20);
    }

    public void getBullets(){
        Bulletup bulletup= (Bulletup) this.getOneIntersectingObject(Bulletup.class);

        if(bulletup != null){
            this.addBullet();
            bulletup = null;
        }
    }

    public void addBullet(){
        if (zaehler_03 > 200){
            magazin += 10;
            zaehler_03 = 0;
        }
    }
    
    public void checkcollision(){
        Meteor meteor = (Meteor) this.getOneIntersectingObject(Meteor.class);

        Dead dead =new Dead();

        if(meteor != null){
            this.getWorld().addObject(dead, 600, 500);
            Greenfoot.stop();
        }
     }
    }

Solution

  • There are two ways main ways of getting variables from another class:

    First way:

    private int YOURVARIABLE = 0;
    public static int getNumber() {
        return YOURVARIABLE;
    }
    public static void setNumber(int x) {
        YOURVARIABLE = x;
    }
    
    // calling the method form another class
    CLASSNAME.setNumber(YOURNUMBER); //setting the number
    CLASSNAME.getNumber(); //getting the number
    

    Second way:

    // You can create a variable like this:
    public static int x = 0;
    // And call it from another class with this:
    CLASSNAME.x