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

2013/8/8

Control the speed of the game? help?

1
2
danpost danpost

2013/8/8

#
You do need a field to control the slow motion portion of the code; however, instead of using two fields (a timer and a flag) using one is easier (a timer; if the timer is not zero, decrement it and if it becomes zero, resume normal speed; if the timer is zero, no action required; when a potato is eaten, set timer to maximum slow time and set slow speed).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// with instance field
private int slowTimer;
// in act method
runSlowTimer();
eat();
// with these methods
private void runSlowTimer()
{
    if (slowTimer > 0)
    {
        slowTimer--;
        if (slowTimer == 0) Greenfoot.setSpeed(50);
    }
}
 
private void eat()
{
    Actor potato = getOneIntersectingObject(Potato.class);
    if (potato != null)
    {
        getWorld().removeObject(potato);
        slowTimer = 5000;
        Greenfoot.setSpeed(25);
    }
}
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Thanks danpost for fixing the code, you are the best! And as Yoda would say, 'You(me) have much to learn young padowing'.
joeschmoe joeschmoe

2013/8/8

#
It works, except it doesnt go back to normal speed...
joeschmoe joeschmoe

2013/8/8

#
I think I understand it now though. I'm going to try it on my own and if I get stuck again I'll come back here
danpost danpost

2013/8/8

#
'Normal' speed would be whatever speed is set by the speed slider in your scenario. In scenarios where you alter the speed, you should set the initial speed in the world constructor; then, use the same value when the 'slowTimer' has expired (reached zero).
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Dan, can you clarify something for me? Here is the link . Thanks in advance!
danpost danpost

2013/8/8

#
What, exactly, do you need clarified?
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
The post at the bottom
danpost danpost

2013/8/8

#
Are you are referring to the casting of a double to an int, which rounds toward zero?
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
Yes, would it do that?
danpost danpost

2013/8/8

#
Test it out. Put the following in any world constructor:
1
2
3
4
5
6
double d = -2.0;
while (d<= 2)
{
    System.out.println(""+d+" rounds to "+((int)d));
    d += 0.1;
}
8bitcarrotjuice 8bitcarrotjuice

2013/8/8

#
I'll try it later, thanks!
8bitcarrotjuice 8bitcarrotjuice

2013/8/9

#
Dan, in the folowing update of my game, I will only use one platform which is set-able to any angle, and using that the cube gets the platforms angle and applies it on itself. This is why I am using the move() method, since it moves the object 'forward' or 'backward' depending on it's rotation. The problem I have is that move() does not accept doubles, and because int's are far too in-precise, I need to use doubles. Is there any method that does the same(or similar) action as move(), but accepts doubles? Thanks in advance!
8bitcarrotjuice 8bitcarrotjuice

2013/8/9

#
Nevermind, I found the smoothmover class...
You need to login to post a reply.
1
2