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

2013/4/8

accessing variable from another class

welleid welleid

2013/4/8

#
Hello, I know this subject is overused and overseen, but I don't know how to do it. I did the tutorial with the asteroids and the counter, but it's not actually exactly what I want. Here is my problem : I have a class called Ball.class, I put an actor in it. In the game, it is possible to add other Ball, but when all the balls have disappear, the game must stop. So basically, I need a variable that increase whenever a ball is added, and that decrease whenever a ball is removed. That's all^^'' Thanks a lot !
danpost danpost

2013/4/8

#
If you just need to keep or check to see how many balls are in the world, you can use the world class methods
int ballsInWorld = getObjects(Ball.class).size();
// or if you only need to know when no balls are in the world
boolean noBalls = getObjects(Ball.class).isEmpty();
From other actor classes, you can prefix. the methods with 'getWorld().' to acquire the same info.
welleid welleid

2013/4/8

#
Thanks it works perfectly ! :) But just for my curiosity, how does the .size() work and where does it come from ? And actually, I need to know how to access a variable in a class from another class really simply. I need to set a int variable in a class with a normal state of 0, and if it turns to 1, I in my Ball.class, I must put an if with its as the condition. Sorry for my bad english^^'
danpost danpost

2013/4/8

#
I would need to know what kind of classes they are (what they extend -- World or Actor); the names of the classes would help; and which one the variable is in and which one you need to get the value of it in.
danpost danpost

2013/4/8

#
'getObjects' returns a List object. Methods for this type of object can be found in the List class documentation.
welleid welleid

2013/4/9

#
okay so basically i have two classes that extends from actor, the one with the variable is called Under.class and the one in which i must access this variable is called Ball.under
danpost danpost

2013/4/9

#
Did you mean 'Ball.class' (not 'Ball.under') and how is the variable declared (give the whole line).
welleid welleid

2013/4/9

#
Youll find a simple If { Int isOnFire = 1; } In the Under.class, and i must access it in the Ball.class
danpost danpost

2013/4/9

#
If you are declaring the 'isOnFire' variable in the method, it will be lost before you get a chance to access it.. It must be declared outside the method (but still inside the class):
// import statement(s)

public class ClassName extends SuperClassName // Actor or World (usually)
{
    private int isOnFire; // declare the field here

    public ClassName() 
    {
        // constructor code
    }

    public void act()
    {
        // act code
    }
    
    // add this method
    public int getIsOnFire()
    {
        return isOnFire;
    }
    // etc.
}
Then a ball object can acquire the value of 'isOnFire' from an Under object; first you need a reference to the Under object in question.
Under under = (Under) getObjectAtOffset(0, getImage().getHeight()/2+1, Under.class);
if (under != null)
{
    if (under.getIsOnFire() == 1) // do one thing
    else // do something else
}
welleid welleid

2013/4/9

#
When I put your linecode in Ball.class ,the "Under under = (Under) getObjectAtOffset(0, getImage().getHeight()/2+1, Under.class); " I have an error message saying "cannot find symbol - method getObjectAtOffset( int, int, java.lang.class<Under>) I think it doesnt find the Under.class, but it exists and I already used other methods like "Under under = (Under)getOneIntersectingObject(Under.class);" without having a problem. But i have no problem compiling the Under.class with the new linecode Thanks for your help !
welleid welleid

2013/4/9

#
no problem i'm just dumb as hell. Thanks for the help man !
danpost danpost

2013/4/9

#
That's my bad! Should be 'getOneObjectAtOffset'.
You need to login to post a reply.