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

2021/1/31

Need help with delay

RcCookie RcCookie

2021/2/1

#
If you want to do this properly you need to keep using System.currentTimeMillis(), but not in a while loop, for example like this:
import greenfoot.*;

public class Job1 extends Job {

    public static final long CUT_TIME_DIF = 1000;
    public static final int MAX_CUT = 6;

    long lastCutTime = 0l;
    int currentCut = 0;
    boolean finishedCutting = false;

    public void act() {
        cut();
        remove();
    }

    public void cut() {
        if(!Greenfoot.isKeyDown("c") || finishedCutting) {
            lastCutTime = 0l;
            return;
        }
        if(System.currentTimeMillis() > lastCutTime + CUT_TIME_DIF) {
            lastCutTime = System.currentTimeMillis();
            currentCut++;
            setImage("cut" + currentCut + ".png");
            if(currentCut = MAX_CUT) finishCutting();
        }
    }

    private finishCutting() {
        finishedCutting = true;
        getWorld().addObject(new Bed(), 200, 200);
    }

    public void maybeRemove() {
        if(Greenfoot.isKeyDown("x")) getWorld().removeObject(this);
    }
}
danpost danpost

2021/2/1

#
XApple15 wrote...
I’d like for aprox 3 fps
Just change all 60 to 3 and the 301 to 16.
RcCookie RcCookie

2021/2/1

#
But its still fixed to a specific framerate. That means if there occures lag it ain't delay 1 second. Also on ~3 fps you won't hit perfect 3 seconds. You are much more precise by actually measuring time
XApple15 XApple15

2021/2/1

#
Alright. Thank you for help !
danpost danpost

2021/2/1

#
RcCookie wrote...
But its still fixed to a specific framerate. That means if there occures lag it ain't delay 1 second. Also on ~3 fps you won't hit perfect 3 seconds. You are much more precise by actually measuring time
At 3fps, if there is any lag, it would be pretty much guaranteed to be a coding issue as it would have the equivalent time of 20 normal act cycles to perform just one. Also, a key could be pressed and released in as quickly as about 1/20 of a second. The user might have to hold a key down an extended amount of time for an action to occur at 3fps.
RcCookie RcCookie

2021/2/4

#
Yes but if you actually are to change the framerate you will be fine. Also I think that it’s just the much more legit version because it’s not hard-coded. And on higher framerates, it will be a lot more precise when lag occurs. If people would get away from using these slow, fixed framerates they could actually get a much smoother experience overall
danpost danpost

2021/2/4

#
XApple15 wrote...
I have like 40 pictures showing one by one with a delay of 0,5 sec , so that’s 2 FPS which is a low framerate , this should not be the issue here
You should probably run your scenario at normal speed (60fps ~ 50speed) and just change the image every 30 frames. For most scenarios, using frames makes for more consistent behavior. Any lag could throw timing of interplay of actors off when using system time.
You need to login to post a reply.