Wrap is a little like mod and limit combined into one. You supply a range, from 0 to 'max', and if this number lies outside of that range, then it will be rolled over until it fits.
For example if the range is from 0 to 5, and this number is 6, then 1 is returned. It wraps around past 5, and counts in from the other side.
This is useful for wrapping a value around the valid indexes, of an array, or wrapping an x and y locations on the screen.
Note that 'max' is always exclusive, whilst 0 is inclusive. 'max' can also be negative.
The same as the previous wrap method, however this allows you to alter the minimum value. Instead of being 0, you can pass in your own.
Technically, the order of 'min' and 'max' does not matter. Either one can be less or greater than the other. However 'min' is always inclusive, whilst 'max' is always exclusive, in the range.
* Keeps the ball between the 'MIN' and 'MAX' bars.
* When it goes outside these, it's x value is wrapped.
x = 0
y = getScreenHeight()/2
inc = 1
MIN = 200
MAX = getScreenWidth() - MIN
onEachFrame() do |delta|
fill( :blue )
if getControls().isKeyPressed( :space )
inc = -inc
x = x + delta*2*inc
x = x.wrap( MIN, MAX )
setColor( :white ) do
fillCircle( x, y, 50, true )
drawLine( MIN, 0, MIN, getScreenHeight() )
drawLine( MAX, 0, MAX, getScreenHeight() )
setColor( :black, 0.6 ) do
fillRect( 0, getScreenHeight(),
getScreenWidth(), 40, :left, :bottom )
fillText( "Press Space to change direction",
10, getScreenHeight()-10, :left, :bottom )