Making an FPS Counter

Материал из Поле цифровой дидактики

FPS (frames-per-second) is a value that shows the current frame rate. Showing the current frame rate can be useful for debugging projects that may affect Scratch's performance.

Making an FPS Variable

Timer in Use Method

If the timer is being used, the following script can be used:

when flag clicked
reset timer
set [Previous Timer v] to (0)
forever
 set [FPS v] to ((1)/((timer)-(Previous Timer)))
 set [Previous Timer v] to (timer)
end

How it Works

When the flag is clicked, the timer is reset, a variable, in this example, "Previous Timer," is set to 0 and and a forever loop starts. In this loop, another variable, in this example, "FPS," is always set to 1 divided by the current value of the timer subtracted by the value of the "Previous Timer" variable, and the "Previous Timer" variable is set to the current timer. After this block's script is run, the timer is reset. The value of the "FPS" variable is roughly the value of the current FPS.

Timer Not in Use Method

Шаблон:Caution The current FPS can be shown with the following script if the timer is NOT being used:

when flag clicked
reset timer
forever
 set [FPS v] to ((1)/(timer))
 reset timer
end

How it Works

When the flag is clicked, the timer is reset and and a forever loop starts. In this loop, a variable, in this example, "FPS," is always set to 1 divided by the current value of the timer. After this block's script is run, the timer is reset. The value of the "FPS" variable is roughly the value of the current FPS.