Hi, so I’m making a game in Processing but unfortunately, I can’t seem to get the program to run smoothly. Sometimes it will stutter while running, while the next time be completely fine even though it is running the same code the whole time and is quite minimal. I have already replace code so that objects are reused, but even just making a single line slowly draw along the screen by shifting x every frame causes stutters.
Not sure if I can upload a whole project here but I’m going to try:
Any help would be greatly appreciated!
I have tried multi-threading which didn’t seem to make it any faster ( thread() for calculating pipe’s positions separately in a while loop while draw() just draws the current state )
To play the game you press ‘S’ to go to the playing mode and press/hold space to actually make the game run
Hi @djx, You’re missing a } in keyPressed(). I ran it, pressed S and s, nothing happened. Guess at why it doesn’t run smoothly: Not completing all the code within the frame period. 1000/60 = 17 mS. (Think you can’t have 17mS only 10 or 20 depending on computer/O.S.)
Sorry, would you mind trying it again? I fixed that error but forgot to upload it. I also progressed more with the design. The lag is still present sometimes (seems to be if you try start the game too quickly) but it is better.
To play the game you press ‘S’ to go to the playing mode and press/hold space to actually make the game run
I have the code running. Can see the stutter in the way the pipes move. Reduced frameRate to 30, removed the plane, top half of pipe, scores. All that’s left is the pipe x calc and the pipe redraw. It still stutters. It gives the appearance of skipping one draw. Hmm…
As the pipes move across, every few seconds they move jumpily, as though 1 frame is missing. I’ve tried it with various frameRates. With 100 or 50 it moves in a constant but slightly rough way. With other values it stutters occasionally. It think the Processing timebase is beating with the PCs timeslicing (10 or 20 mS). Sometimes it runs smoothly for a couple of seconds - how does it do that and why can’t do that continuously? I’ve tried turning the priority up in task manager, didn’t help.
When you use frameRate(60), processing does its best to keep it constant but it is not guaranteed.
Instead what you should do is get the elapsed time between each frame and pass it as an argument to your “move” functions. Then instead of moving by a set amount, you can move depending on the elapsed time.
Look at the example, Topics, Advanced Data, Create Shapes, PolygonPShapeOOP3. It has more moving items, and it’s perfectly smooth. What differences between that and Wings? Wings doesn’t have P2D and it uses images (maybe +others). Try putting P2D into Wings - stutters very occasionally.