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

2020/10/5

Reading the value of the speed slider

RcCookie RcCookie

2020/10/5

#
I would like to get a value which describes the current simulation speed. Something like
1
int currentSpeed = Greenfoot.getSpeed();
but that does not exist. Is there a sneaky way to read the current speed out of the greenfoot core? If you wonder: I am trying to reuse that slider for my own purpose in the game. The game itself runs independent of the actual speed anyways.
danpost danpost

2020/10/5

#
RcCookie wrote...
I would like to get a value which describes the current simulation speed
Just add an int field to your world set to 50:
1
public int simulationSpeed = 50;
and also add the following methods:
1
2
3
4
5
6
7
8
9
10
11
12
public int getSimulationSpeed()
{
    return simulationSpeed;
}
 
public void setSimulationSpeed(int speed)
{
    if (speed < 1) speed = 1;
    if (speed > 100) speed = 100;
    Greenfoot.setSpeed(speed);
    simulationSpeed = speed;
}
Add the following line to your world constructor to ensure the field is in sync with the actual speed:
1
setSimulationSpeed(simulationSpeed);
danpost danpost

2020/10/5

#
RcCookie wrote...
Is there a sneaky way to read the current speed out of the greenfoot core?
The "sneaky" way is this (after a little investigating and testing):
1
int simulationSpeed = greenfoot.core.Simulation.getInstance().getSpeed();
RcCookie RcCookie

2020/10/5

#
danpost wrote...
RcCookie wrote...
Is there a sneaky way to read the current speed out of the greenfoot core?
The "sneaky" way is this (after a little investigating and testing):
1
int simulationSpeed = greenfoot.core.Simulation.getInstance().getSpeed();
This is what I'm looking for, thanks! The other way does not work if you change the setting on the slider manually, it only works when setting it using code.
danpost danpost

2020/10/5

#
RcCookie wrote...
The other way does not work if you change the setting on the slider manually, it only works when setting it using code.
True.
You need to login to post a reply.