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

2023/6/5

How to draw integers

Deepfist Deepfist

2023/6/5

#
I have a player actor who has multiple integer values, such as the amount of medkits, the amount of multishots left, the amount of bullets left in a specific magazine, the remaining speedUp time and the score. These integers work fine, but I am not sure how to show them on screen with their right amount. These integers are in the player.class, but their values are changed by different actors; so when you pick up a medkit then the medkit.class will add 1 medkit to the count, not the player.class itself (this is done with a getOneIntersectingObject code, not sure if that's important or not). I also have multiple world (such as a win and a lose world) and in those worlds I also want to show the code. For text I currently use the showText option, but I'm not sure if this works with integers; I don't like the look of it anyways and I can't change it, so If someone knows an option for that then I would appreciate that too. If you need any code then ask for that.
danpost danpost

2023/6/5

#
Deepfist wrote...
I have a player actor who has multiple integer values, such as the amount of medkits, the amount of multishots left, the amount of bullets left in a specific magazine, the remaining speedUp time and the score. These integers work fine, but I am not sure how to show them on screen with their right amount. These integers are in the player.class, but their values are changed by different actors; so when you pick up a medkit then the medkit.class will add 1 medkit to the count, not the player.class itself (this is done with a getOneIntersectingObject code, not sure if that's important or not).
For each value, add a method to adjust its value. For example:
// let us say you have
private int medkits = 0;

// then, add this method:
public void adjustMedKits(int change) {
    medkits += change;
    /** update displayed number of medkits */
    // updateDisplayedValues();
}
For some value, the method can restrict what the value can be and/or test the value for specific goals, or to perform some action(s) based on its new value. Line 8 refers to a method you can add that displays all the pertinent values and can be called by all these new methods. Of course, you could display each value individually with separate methods or display them all together.
I also have multiple world (such as a win and a lose world) and in those worlds I also want to show the code.
The values required by the win/lose worlds can easily be passed to those worlds by way of the constructor's argument list. For example:
// if you have
public WinWorld()

// change it to
public WinWorld(int score)
Then, when creating the world:
Greenfoot.setWorld(new WinWorld(score));
For text I currently use the showText option, but I'm not sure if this works with integers; I don't like the look of it anyways and I can't change it, so If someone knows an option for that then I would appreciate that too.
Please refer to my Value Display Tutorial scenario.
You need to login to post a reply.