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

2017/5/22

Run a Function Ever Other Second

Alola_Sandslash Alola_Sandslash

2017/5/22

#
I have a gravity function that I want to run every other second so that the player doesn't fall as fast. Is there anyway I can do this? Maybe a different way is easier?
FutureCoder FutureCoder

2017/5/22

#
whats the code for the gravity, it will be useful to know the code you're using
Super_Hippo Super_Hippo

2017/5/22

#
If by 'second', you mean 'act-cycle', then it would look like that:
private int gravityTimer = 0;

//in act method
if (++gravityTimer == 2)
{
    gravityTimer = 0;
    //call the gravity method here
}
However, you could also let your object fall the half way every act-cycle (maybe need to import the SmoothMover class).
Yehuda Yehuda

2017/5/22

#
If you want the actor to fall slower then you should just lower the gravity (instead of subtracting every other run).
danpost danpost

2017/5/23

#
Yehuda wrote...
If you want the actor to fall slower then you should just lower the gravity (instead of subtracting every other run).
Agreed -- but not so easy if gravity is already at a value of '1'. Then, you would either have to track speed and gravity using double or float type values (so you can lower gravity to a non-integer value); or, you can divide the change in vertical location in half (keeping gravity and jump strength at their current values): For example:
vSpeed += 1; // gravitational pull
setLocation(getX(), getY()+vSpeed/2); // fall (or rise)
Yehuda Yehuda

2017/5/23

#
Another possibility is to lower the execution speed.
danpost danpost

2017/5/23

#
Yehuda wrote...
Another possibility is to lower the execution speed.
Unfortunately, that would effect all the other moving actors as well. I do not think that is what is wanted here. And if lowered too much, the moving actors would move less smoothly (you would begin to see each individual movement step in the actors).
Yehuda Yehuda

2017/5/23

#
Right, but if the execution was let's say 45 ever since the creation of the scenario then the lowest possible speed is lower then it was before. I agree to use that as a solution here wouldn't work too great.
danpost danpost

2017/5/23

#
danpost wrote...
or, you can divide the change in vertical location in half (keeping gravity and jump strength at their current values): For example:
vSpeed += 1; // gravitational pull
setLocation(getX(), getY()+vSpeed/2); // fall (or rise)
Forget this way. I doubt it will work properly.
Alola_Sandslash Alola_Sandslash

2017/5/23

#
I want the gravity to either be less than one since it currently is one or for the gravity method to be called every other second. neither of the suggestions currently works.
Super_Hippo Super_Hippo

2017/5/23

#
Show what you have tried to implement what I suggested.
Alola_Sandslash Alola_Sandslash

2017/5/23

#
Yes I tried that but nothing changed.
Super_Hippo Super_Hippo

2017/5/23

#
Show your attempt, so your code (with code tags -- see below the reply box).
danpost danpost

2017/5/23

#
Use double values. Make gravity '0.25' and divide jump speed in half. Also, track the x and y coordinates of the player using double values so there will be no loss in precision when applying such small changes in location (since greenfoot tracks the location of actors using int values). This change is basically what the SmoothMover class (provided by greenfoot) provides to its subclasses.
You need to login to post a reply.