How to stop a game object leaving the screen

Hi,

I was wondering how i could stop my player from leaving the screen? it is controlled using the arrow keys and I don’t want it to move off the screen when my game is inplay.

Thanks

One option is to check the position and if it is outside screen bounds you reset the position. Like this:

function update(self, dt)
	local pos = go.get_position()
	local width, height = window.get_size()
	if pos.x < 0 then
		pos.x = 0
	elseif pos.x > width then
		pos.x = width
	end
	if pos.y < 0 then
		pos.y = 0
	elseif pos.y > height then
		pos.y = height
	end
	go.set_position(pos)
end

Brilliant, thank you!