okay, so after some trial and error I've got my space invaders game running smoothly on 60hz and 120hz settings. I added this function to calculate the current average screen update rate:
first, declare a float in globals called delta
then add this sub:
Sub UpdateDelta
' Add the current frame's delta time to the list
deltaTimes.Add(Round2(lGdx.Graphics.DeltaTime,3))
' Keep only the last 100 delta times
If deltaTimes.Size > 100 Then
deltaTimes.RemoveAt(0)
End If
' Calculate the average of the delta times
Dim total As Float = 0
For Each deltaTime As Float In deltaTimes
total = total + deltaTime
Next
averageDeltaTime = total / deltaTimes.Size
' Log the average delta time
Log("Average Delta Time : " & Floor(averageDeltaTime*1000))
delta = Round2(averageDeltaTime*60,2)
Log ("Delta = " & delta)
End Sub
Delta is now a float where 1 is 60fps, 0.5 is 120fps, 0.666 is 90hz, etc.... So in the gameplay loop, I now multiple any movement value by delta which accounts for the change in the refresh rate. for example, if my player moves by doing "player.x = player.x + mx", then I change this to "player.x = player.x + mx*delta"
As the delta function is taking an average value, it accounts for any tiny blips in framerate from other apps taking CPU time, so things stay smooth.
So for movement values, multiplying by delta will reduce them for 120hz displays, but then when I have values that decrease by one for each frame, like an explosion life, or a countdown, I now divide by the delta. In my invaders game, I would create score messages on screen to show when the player shoots an alien, this normaly fade out over the next 40 frames, so now I divide this by delta when I create the value, so it takes more frames to disappear. I do the same for other vlaues that are affected by the "frame" value, such as how quickly the background scrolls, or what animation frame the characters are on. My aliens are drawn with the frame number selected based on (frame / 120 mod 2) meaning it will be either 0 or 1, changing every 60 frames, I now update frames by speed*delta so the frames only go up half as fast when at 120hz.
I'll try to get the flappy bird code updated, tidied up and fixed to use this method in the next few days.
There's probably a better way to do this, but this is how I've managed to do it. I'll update Invaders on the store soon and will need people to test on 90hz devices.
@ilan I checked your Android games at 120hz. Pixel knight runs fine, it's just the animation frames that run too fast on the sprites, so I'd image setting the "frames" variable to float, and then updating it by speed*delta should fix this.
@melonZgz All your games run fine at 120hz and look really nice and smooth, how did you manage this???
Note: basically anything involved in the speed of anything in your game now needs to be a float type. I'll republish the cloney bird tutorial next week with source code and youtube video explianer.
I still think B4A with Libgdx is a good tool to use for simple 2D games. It has box2d (which I've not tried), and
@melonZgz games prove that these can look very professional.
I know we also have x2 but honestly I've not really tried it, if someone wants to doe a flappy bird tutorial using x2 then I'd be interested to see it.