Clamp function not working

Hi,

I can't get the clamp function to work. Here's my code on windwos:

gfx = playdate.graphics
playdate.display.setRefreshRate( 50 )

x1 = 0
x2 = 400
y1 = 0 
y2 = 240

sx = 0
sy = 0 
sw = 40 
sh = 40
sel = playdate.geometry.rect.new(sx,sy,sw,sh)


function playdate.update() 
	gfx.clear()
	math.clamp (sx, 0, 360)
	
	gfx.drawRect(sx,sy,sw,sh)
	
	for i = 0, 400, 40 do 
		gfx.drawLine(i, y1, i, y2)
	end
	for i = 0, 240, 40 do 
		gfx.drawLine(x1, i, x2, i)
	end
	
	if playdate.buttonJustPressed(playdate.kButtonRight) then 
		print (sx)
		sx += 40 
	end
	if playdate.buttonJustPressed(playdate.kButtonLeft) then 
		print (sx)
		sx -= 40 
	end
end

function math.clamp(a, min, max)
    if min > max then
        min, max = max, min
    end
    return math.max(min, math.min(max, a))
end

Thanks

I think sx = math.clamp(sx, 0, 360) is what you're going for :slight_smile:

1 Like

Awesome. Thank you Dave. Works now.