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

2017/10/27

Is accessing other classes' integers possible?

monmonmon monmonmon

2017/10/27

#
Hi, so I successfully made an ammo system and I also have a score counter, and I saw from my score class that makes the number of the integer score display in the image created ("Score : " + score) something like this, I tried to imitate this code but the ammunition integer is at the person class, I want to know how can I access other classes' integers, if it's possible
danpost danpost

2017/10/27

#
monmonmon wrote...
I successfully made an ammo system
What does it consist of? -- show code.
and I also have a score counter, and I saw from my score class that makes the number of the integer score display in the image created ("Score : " + score) something like this, I tried to imitate this code but the ammunition integer is at the person class, I want to know how can I access other classes' integers, if it's possible
Of course, it is doable. Will also need to see class of person and probably your World subclass as well.
monmonmon monmonmon

2017/10/29

#
public class AmmoCount extends Text
{

public void act() 
    {
        setImage(new GreenfootImage("Ammo : " + ammunition, 24, Color.YELLOW, Color.BLACK));
//ammunition integer is the one I wish to access
    }  
}



public class Person extends MainActors
{

int ammunition = 75;

public void act() 
    {
           pickUpAmmo();
}

public void pickUpAmmo()
    {
        Actor ammo = getOneIntersectingObject(Ammo.class);
        
        if (ammo != null)  
        {
            ammunition=ammunition+5;
            getWorld().removeObject(ammo);
        }
    }
}



public class MyWorld extends World
{
    Counter counter = new Counter();    
    HealthBar healthbar = new HealthBar();

public MyWorld()
    {    
        super(600, 400, 1); 
        addObject(counter, 100, 40);
        addObject(healthbar, 200, 40);
}

public Counter getCounter()
    {
        return counter;
    }
    
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
I tried once adding the return statement with the person but it didn't work so I removed it in the world subclass
danpost danpost

2017/10/29

#
An 'ammunition' integer will not exist until you create a Person object. Add a Person reference in your MyWorld class and a getter method to go along with it (like you did with the health bar and counter objects). You can add the person into the world in the constructor as well. Once that is done, post your revised MyWorld class code to continue from.
monmonmon monmonmon

2017/10/30

#
public class MyWorld extends World
{
    Counter counter = new Counter();    
    HealthBar healthbar = new HealthBar();
    Person person = new Person();
 
public MyWorld()
    {    
        super(600, 400, 1); 
        addObject(counter, 100, 40);
        addObject(healthbar, 200, 40);
}
 
public Counter getCounter()
    {
        return counter;
    }
     
public HealthBar getHealthBar()
    {
        return healthbar;
    }

public Person getPerson()
    {
        return person;
    }
}
danpost danpost

2017/10/30

#
You still did not add any Person actor into your world. Once you do, you can use:
int ammoCount = ((MyWorld)getWorld()).getPerson().ammunition;
from any Actor object that is in the world..
monmonmon monmonmon

2017/11/1

#
Oh okok thanks
monmonmon monmonmon

2017/11/1

#
I get an error "java.lang.NullPointerException at AmmoCounter.<init>(AmmoCounter.java:11) at MyWorld.<init>(MyWorld.java:12)" And I already added the Person actor in the world
danpost danpost

2017/11/1

#
monmonmon wrote...
I get an error "java.lang.NullPointerException at AmmoCounter.<init>(AmmoCounter.java:11)
Need to see entire AmmoCounter class code.
You need to login to post a reply.