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

2013/10/3

Change variable value in another class

DownKost DownKost

2013/10/3

#
Well, i have a variable called life, and life = 5, i want to do life--; in another class, how do i do it?
JetLennit JetLennit

2013/10/3

#
What is the name of the class life is in?
DownKost DownKost

2013/10/3

#
JetLennit wrote...
What is the name of the class life is in?
player
JetLennit JetLennit

2013/10/3

#
But this in the class that you want to have the life--;
player p = new player()
p.life--;
danpost danpost

2013/10/3

#
@JetLennit, that will create a new player instance and decrease its 'life' value. I am sure that DownKost wants to change the life value of the player object that is already in the world (not a different one). You need to get a reference to your player object (the one that you wish to decrement the life of). If you only have one player, then '((Player)getWorld().getObjects(Player.class).get(0)).life--' . If 'life' is a private field, you will need to have 'get'/'set' or 'add' method(s) for the field and use them to change its value instead of changing the value directly but you would still get the reference to the object the same way (if you do not already have a reference to that player object.
DownKost DownKost

2013/10/3

#
Thank you very much
You need to login to post a reply.