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

2012/10/24

get Variables from other classes

1
2
HamburgerSV HamburgerSV

2012/10/24

#
I am looking for a solution for my problem. I want to have a class, which shows the points. But the point-int-variable is in another class. can someone help me? thanks
Gevater_Tod4711 Gevater_Tod4711

2012/10/24

#
You need to get the referenc to the object with the variable you want to use. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
//in the counter class;
public void act() {
    if (!getWorld().getObjects(theOtherClass.class).isEmpty()) {
        int points = getWorld().getObjects(theOtherClass.class).get(0).getPoints();//int points is the variable in this class where the points are saved;
    }
}
 
//in the other class;
...//some other stuff;
 
public int getPoints() {
    return points;//or however your variable is called;
}
HamburgerSV HamburgerSV

2012/10/24

#
ok thank you :) I will try it.
HamburgerSV HamburgerSV

2012/10/24

#
it doesnt work :( is there a difference if the variable is in a subclass?
Gevater_Tod4711 Gevater_Tod4711

2012/10/24

#
No there should be no difference but if you show us the code we could easyer help you.
HamburgerSV HamburgerSV

2012/10/24

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Robson extends Roboter
{
        public int points = 0;
               int SPEED = 1;
               int MaxPoints = 153;
               int Bombs = 2;
            
  
 
 
    public void act()
    {
         win();
         checkKeys();
         freeze();
    }
    
    public int getPoints()
    
        return points;
    
.............
}
 
 
 
public class Counter extends Robson
{
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public void act()
    if (!getWorld().getObjects(Robson.class).isEmpty())
    
        int points = getWorld().getObjects(Robson.class).get(0).getPoints(); 
    
}
cannot find symbol- methode getPoints()
Gevater_Tod4711 Gevater_Tod4711

2012/10/24

#
ok you could try this:
1
2
3
4
5
6
7
8
9
public void act()  
{   
    if (!getWorld().getObjects(Robson.class).isEmpty())  
    {
        List<Robson> robsons = getWorld().getObjects(Robson.class);
        int points = robsons.get(0).getPoints();   
    }   
}   
}
but to use this you have to import java.util.List;
HamburgerSV HamburgerSV

2012/10/24

#
Thank you very much:) no syntax errors :)
Gevater_Tod4711 Gevater_Tod4711

2012/10/24

#
I'm glad that I could help you.
danpost danpost

2012/10/24

#
You could change line 37 to the following (without having to import java.util.List
1
int points = ((Robson) getWorld().getObjects(Robson.class).get(0)).getPoints();
joris0127 joris0127

2014/1/22

#
what does the get(0) mean?
bourne bourne

2014/1/22

#
After typing the following,
1
int points = ((Robson) getWorld().getObjects(Robson.class).
Press control+space, Then begin typing "get" and it should display info for that method Alternatively, you could notice what type getObjects(Class) returns (-->List), then googling "java api List", should be the first link, scroll down to the method summary section, then find your method.
Berserkker Berserkker

2016/2/25

#
I have a question why this does not work for me: so lobstercounter is a subclass of lobster and this is his code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*; 
import java.awt.*;
 
public class lobstercounter extends lobster
{   
    private String text;
    private GreenfootImage myImage;
    public int wormlobstervalue;
     
    public void act() {
        myImage = new GreenfootImage(30,30);
        setImage(myImage);
        myImage.setFont(new Font("SansSerif",Font.BOLD,14));
        myImage.setColor(Color.BLACK);    
        if (!getWorld().getObjects(lobster.class).isEmpty())
        {
            int wormlobstervalue = ((lobster) getWorld().getObjects(lobster.class).get(0)).getPoints();
        }
         
        text = "" + wormlobstervalue;
        myImage.drawString(text,5,18);
   }         
}
the code of lobster is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class lobster extends Animal
{
     public int wormlobster;
     /**
     * Act - do whatever the lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
...
...
       else if (canSee(worm.class))
       {
           eat(worm.class);
           wormlobster++;
       }
    }
     
    public int getPoints() {
        return wormlobster;
    }
}
if i run int getPoints after i've eaten a worm it returns 1, but the counter doesn't change. I also created a public int in lobstercounter but this one returned 0.
Super_Hippo Super_Hippo

2016/2/25

#
You should've created a new discussion thread. Problem is that 'wormlobstervalue' in line 17 is not the same as in line 20. Remove either line 8 or the 'int' in line 17 and put lines 20 and 21 right after 17.
danpost danpost

2016/2/25

#
For one thing, a lobstercounter object is not a lobster object -- or, more precisely, a lobster counter is not a lobster (nor is it an Animal object, which the lobster class extends). Therefore, your class structure is flawed. The lobstercounter class should not extend the lobster class. You can place the lobstercounter class within the lobster class and have it extend a (basic) counter class (if you have multiple counters in your project) or the Actor class. Then the lobster can create its own counter when it is being created and add it into the world when it is added into the world (using the 'addedToWorld' method -- see the Actor class API). As far as the counter not working, I do not see where you are using 'new lobstercounter()' within your code.
There are more replies on the next page.
1
2