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

2017/10/10

Making a object useable after some acts

Welli123 Welli123

2017/10/10

#
Hi there, I‘m making a space game and it went pretty good so far. The player(spaceship) can build a spacestation at the current location and after 20 acts it is completely build but I’m not sure how i make it usable like when I’m on the station I get some fuel. I just added a image changing method from a unfinished station to the finished one. I would be happy If u have suggestions how I could do this :)
danpost danpost

2017/10/10

#
A space station will have different states -- how much fuel it has would be one of them; whether its build has been completed or not would be another. When programming using OOP, the state of objects are described by the value of fields given to the objects. You might have the following:
private boolean buildCompleted;
private int fuel;
The default values are, consecutively, 'false' and '0'. Obviously, you would not put fuel in an incomplete station; so, when the station is completed (the 20 act cycles have passed), set 'buildCompleted' to 'true' and change the image of the station. You can start adding fuel at that time. Since fuel tanks usually have a specific size and can only hold so much fuel, you can have a constant field to hold the maximum amount of fuel that the station can carry -- for example:
private static final int MAX_FUEL = 50000;
Then, use this to limit the fuel on the station. The space ship will also have a limited size fuel tank -- and could have a similar type constant field. The space station class should have a method to dump fuel from the station. Maybe best is to request a certain amount and it returns how much of the requested amount it can (does) supply. This would look something like this In class of space ship, when requesting fuel (assuming docked at station 'station'):
fuel += station.dumpFuel(MAX_FUEL - fuel);
In class of space station:
public int dumpFuel(int amount)
{
    amount = Math.min(amount, fuel);
    fuel -= amount;
    return amount;
}
You could even regulate the flow of the dumping of the fuel -- maybe using something like this:
fuel += station.dumpFuel(Math.min(50, MAX_FUEL - fuel));
Welli123 Welli123

2017/10/10

#
You wrote some pretty good ideas and instructions :) haha but I thought that the station produce fuel for the spaceship Anyway thank u for ur help I’m working on it :)
You need to login to post a reply.