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

2016/3/7

Instance variable across multiple classes

Bassusour Bassusour

2016/3/7

#
Hi again. So, is it possible to use an instance variable, defined in another class, to use it in an if-statement, in the second class?
danpost danpost

2016/3/7

#
Bassusour wrote...
is it possible to use an instance variable, defined in another class, to use it in an if-statement, in the second class?
Of course. The value of the instance variable just needs to be made accessible to the other class, provided you have or can get a reference to the object that holds that instance variable. You could make the access modifier of the instance variable 'public'; however, in the interest of "proper" coding, keeping it 'private' and adding a 'public' method that returns the value would be the way to go.
Bassusour Bassusour

2016/3/8

#
Could you please show me the code for that? I tried, but it didn't accept my code
1
2
3
4
public void fd_special_spist()
    {
        ormeSpist;
    }
Super_Hippo Super_Hippo

2016/3/8

#
If you have this in class A
1
2
3
4
5
6
private int xyz;
 
public int getXyz()
{
    return xyz;
}
Then you only need a reference in class B to call the method on it. If there is only one A object in the world, it can look like this:
1
A a = (A) getWorld().getObjects(A.class).get(0);
If the method should be executed when B touches A:
1
A a = (A) getOneIntersectingObject(A.class);
Other possibilities are similar. Then, you can easily call the method to get the value:
1
2
3
4
5
if (a != null) //only when it touches A for example
{
    int zyx = a.getXyz(); //or whatever you want to do with it
    //...
}
Bassusour Bassusour

2016/3/8

#
Thanks for helping, Super_Hippo, but how do I set your code properly into my class? My instance variable is named ormeSpist, which is located in the class Player_1 I want to use this variable in the class ND_dum here is where I've gotten
1
2
3
4
5
6
7
8
public void nd_missil()
    {
        Player_1 a = (Player_1) getWorld().getObjects(Player_1.class).get(0);
        if (//I want it to continue if ormeSpist == 3)
        {
            getWorld().addObject (new ND(), getX(), getY());
        }
    }
Super_Hippo Super_Hippo

2016/3/8

#
If 'ormeSpist' is public, then you can use
1
if (a != null && a.ormeSpist == 3)
or just (if there is always one Player_1 in the world)
1
if (a.ormeSpist == 3)
If you want 'ormeSpist' to be a private variable, then you need a getter-method in the Player_1 class:
1
2
3
4
public int getOrmeSpist()
{
    return ormeSpist;
}
And then you can use
1
2
3
if (a != null && a.getOrmeSpist() == 3)
//or
if (a.getOrmeSpist() == 3)
Bassusour Bassusour

2016/3/9

#
Thank you Super_Hippo :). However, this will lead me into a new question. How do i make the if-statement execute only once if true? Because it will spawn hundreds if the statement is true, and I only want it to spawn one instance
danpost danpost

2016/3/9

#
Bassusour wrote...
Thank you Super_Hippo :). However, this will lead me into a new question. How do i make the if-statement execute only once if true? Because it will spawn hundreds if the statement is true, and I only want it to spawn one instance
The problem is that you are not being specific enough about when you want to spawn them. That is, the conditions that 'a' exists and its value of 'ormeSpist' is three is too loose. Another condition needs to be applied to prevent multiple spawnings. One possible extra condition might be that no ND object is currently in the world:
getWorld().getObjects(ND.class).isEmpty()
You need to login to post a reply.