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

2013/1/2

Greenfoot.getSpeed?

erdelf erdelf

2013/1/2

#
is there a way to get the speed of the scenario. so something that would return the value which I can set in this command:
Greenfoot.setSpeed(X);
Oxy Oxy

2013/1/2

#
If your trying to change how fast the Scenario is running there is a bar on the bottem of your scenarios window that you can move left and right to change the speed. if your talking about the speed of a Actor, the main method for moving is move(); just increase the number withen the brackets. If none of my two solutions were about what your talking about please make what you mean by speed more specific
vonmeth vonmeth

2013/1/2

#
The setSpeed method accepts values between 1 and 100. There is method for getting the current speed as far as I know.
erdelf erdelf

2013/1/2

#
well, I want to get the speed of the scenario, I dont want to change it, just get it.
Busch2207 Busch2207

2013/1/2

#
@ Oxy: I'm quite sure, that erdelf knows about the bar! He's asking about a method to get the actual speed... @ erdelf: I have already searched for such a method... And I haven't found one... So I think, there is no... But you can create one by yourselve with some tricks... e.g. you can create a class that saves the speed, like this:
import greenfoot.*;

public class GreenfootSpeed
{
        private static int i_s_Speed=50;

        public static void set(int i_newSpeed)
        {
                Greenfoot.setSpeed(i_newSpeed);
                i_s_Speed=i_newSpeed;
        }

        public static int get()
        {
                return i_s_Speed;
        }
}
and then use:
GreenfootSpeed.set(X);
instead of:
Greenfoot.setSpeed(X);
And you can get the current Speed with:
GreenfootSpeed.get();
Or maybe you have some other suggestions to solve this problem! ;)
vonmeth vonmeth

2013/1/2

#
Sorry, bad typo hah. "There ISN'T a method for getting the current speed as far as I know." is what i meant.
danpost danpost

2013/1/3

#
You do not have create a seperate class to deal with the lack of a 'getSpeed' method in Greenfoot. Just set up a static int field in your world class to hold the current speed. Set the speed you want to start with in the constructor of the world and set the value of the field to that speed. Then you can access that value from any class in your scenario by using the class name, followed by a dot (.), followed by the field name. Make sure you adjust its value any time you change the speed of the scenario. With a world name of 'MyWorld and a static field name of 'scenarioSpeed'
// class field in 'MyWorld' class
public static int scenarioSpeed; 
// in world constructor
scenarioSpeed = 60; // or starting speed
Greenfoot.setSpeed(scenarioSpeed);

// then let's say in an Actor class
if(condition == true)
{
    if(MyWorld.scenarioSpeed < 100) 
    {
        MyWorld.scenarioSpeed++;
        Greenfoot.setSpeed(MyWorld.scenarioSpeed);
    }
}
danpost danpost

2013/1/3

#
It can also be accomplished without the field being of static type (a class field). As an world object instance field it can be accessed with
// instance field of world
public int scenarioSpeed;
// in world constructor
scenarioSpeed = 60;
Greenfoot.setSpeed(scenarioSpeed);
// example usage in an Actor class
if(condition == true)
{
    MyWorld mw = (MyWorld)getWorld();
    if(mw.scenarioSpeed < 100)
    {
        mw.scenarioSpeed++;
        Greenfoot.setSpeed(mw.scenarioSpeed);
    }
}
Busch2207 Busch2207

2013/1/3

#
Well, of course you don't have to create an extra class, but in my opinion it's clearer and easier to handle. :)
You need to login to post a reply.