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

2015/3/24

Call a Actor method to the World

0gener 0gener

2015/3/24

#
I bet it's simple but im having trouble in printing my player's hp in the world... I have the method that returns the player hp in the player class:
public int getHp()
{
     return hp;
}
And I want to call that method in the world and print it for example in the middle, by the way the hp isnt allways the same. Appreciate any help.
danpost danpost

2015/3/24

#
If you have the world call that method to get the value and display it in the world, the display would have to be updated constantly, whether the value of 'hp' changes or not. This is wasteful as you only need to update it when there is a change in the 'hp' value. You could just have the player control the displaying of the value. For example:
public void adjustHealth(int amount)
{
    hp += amount;
    getWorld().showText("HP  "+hp, getWorld().getWidth()/2, getWorld().getHeight()/2);
}
0gener 0gener

2015/3/24

#
Oh you're right... and if I want to change the type or color of the font? How can I do that
danpost danpost

2015/3/24

#
I have not had a chance to experiment with the 'showText' method (I am still using greenfoot USB-2. 3. 0). However, I kind of get the feeling that there is no way to change how the text is displayed using that method. You could try right-clicking on the text while the scenario is stopped to see if you can 'Inspect' it or execute any methods on it. I am interested in what you may find in this regard. Please describe exactly what happens.
0gener 0gener

2015/3/24

#
I just tried it and the text looks like it's not an object. I click on it and appears the world inspect and not the text.
danpost danpost

2015/3/24

#
0gener wrote...
I just tried it and the text looks like it's not an object. I click on it and appears the world inspect and not the text.
Alright. The only suggestion that may or may not help is to change the font of the background image of the world.
0gener 0gener

2015/3/24

#
I'll put the text somewhere the user can see, thanks for helping me.
You need to login to post a reply.