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

2014/10/14

Access and change a variable from another actor

TheUnknown TheUnknown

2014/10/14

#
Hi. How can I access and change the variable "Lifes" in the class "Healthbar" from the class "Player"? I've already tried some solutions, but they did not work for me.
danpost danpost

2014/10/14

#
Please show what you have tried.
Super_Hippo Super_Hippo

2014/10/14

#
If the variable "Lifes" (which should be "lives", starting with small letter and lives and the plural of life) is 'public static', then you can just use 'Healthbar.lives'. If it isn't static, but public, then you need to get a reference to the object. You can either save the reference when adding the object or you can get the Healthbar object with something like this:
1
getWorld().getObjects(Healthbar.class).get(0).lives
This will only work correctly when there is only one Healthbar object. If the variable is private, then you need a getter-method to access it from outside.
1
2
3
4
public int getLives()
{
   return lives;
}
Then you use the following:
1
getWorld().getObjects(Healthbar.class).get(0).getLives()
TheUnknown TheUnknown

2014/10/14

#
Thanks. Could you please show how to use this exactly. I tried
1
int lives=getWorld().getObjects(Healthbar.class).get(0).lives;
in the Player class but i just get an error when I'm triing to compile: cannot find symbol- variable lives. (Sorry for my bad english, it's not my primary language.)
Super_Hippo Super_Hippo

2014/10/14

#
The name of your variable is "Lifes", I only suggested to change it to "lives".
TheUnknown TheUnknown

2014/10/14

#
I already changed the name of the variable.
Super_Hippo Super_Hippo

2014/10/14

#
So in your "Healthbar" class, you have this line?
1
public int lives;
TheUnknown TheUnknown

2014/10/14

#
Yes, but it already has a value:
1
public int lives=3;
TheUnknown TheUnknown

2014/10/14

#
This is the full code. Player:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
 
public class Player extends Actor
{
   private Pfeil meinPfeil;
   int schussabstand=0;
   int lives=getWorld().getObjects(Healthbar.class).get(0).lives;
    
    /**
     * Act - do whatever the Spieler wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act()
   {
       getMouse();
       bewegen();
       schießen();
        
   }   
   public void bewegen()
   {
        turnTowards(MouseX, MouseY);
              
       if (Greenfoot.isKeyDown("w"))
         {
            move(4);
         }
          
       if (Greenfoot.isKeyDown("s"))
         {
            move(-4);
         }
                                
   }
    public void schießen()
     
   {
       
    
          
         {
             
            if(schussabstand > 0)
            {
                schussabstand -= 1;
            }
            if (Greenfoot.isKeyDown("Space") && (schussabstand == 0))
            
                getWorld().addObject(new Pfeil(getRotation()), getX(), getY());
                schussabstand = 40;
            }
             
         
       
        int SpielerX=getX();
        int SpielerY=getY();
         
         
         
   }
    
   public static int MouseX = 0, MouseY = 0;
   private void getMouse()
   {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if ( mouse != null )
        {
            MouseX = mouse.getX(); MouseY = mouse.getY();
        }
   }
      
}
Healthbar:
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
41
42
43
44
45
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Healthbar here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Healthbar extends Actor
{
    public int lives=3;
    public int getLives() 
    
        return lives; 
    
 
    /**
     * Act - do whatever the Leben wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Leben();// Add your action code here.
    }
 
    public void Leben()
    {
        if(lives==3)
        {
            setImage("Lebensanzeige3.png");
        }
        if(lives==2)
        {
            setImage("Lebensanzeige2.png");
        }
        if(lives==1)
        {
            setImage("Lebensanzeige1.png");
        }
        if(lives==0)
        {
            setImage("Lebensanzeige0.png");
        }
    }
}
davmac davmac

2014/10/14

#
I think you need a cast. In the original line:
1
int lives=getWorld().getObjects(Healthbar.class).get(0).lives;
The return type from 'get(0)' is Object, which has no lives. Cast it to HealthBar:
1
int lives=((HealthBar)(getWorld().getObjects(Healthbar.class).get(0)).lives;
TheUnknown TheUnknown

2014/10/15

#
Now it says it expects a ")", but after i changed this, I got the same error as before: cannot find symbol- variable lives.
danpost danpost

2014/10/15

#
There is an extra (or unbalanced) parenthesis in what was given. It should have been this:
1
int lives = ((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).lives;
TheUnknown TheUnknown

2014/10/15

#
Thanks danpost. Now I have no compiling errors but I'm getting a null pointer exception.
Super_Hippo Super_Hippo

2014/10/15

#
I think you can't use methods like 'getWorld' outside methods, because these lines will be executed before the object is placed to the world → getWorld returns null. Try to use it where you want to use it. If you want to have a variable that stores the value which the 'lives' variable in the Healthbar class had, when the Player-object was placed, then declare the variable outside methods and put the given line without the 'int' at the beginning in the 'addedToWorld' method. If you want to get the current value of the 'lives' variable, then you need to place the line where you want to get it. If you want to change it (for example you are losing a life), you can use this:
1
((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).lives--;
TheUnknown TheUnknown

2014/10/15

#
Thank you very much! Everything works perfectly!
You need to login to post a reply.