What's the best way to make boundaries around the screen?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
What's the best way to make boundaries around the screen?
Just a quick question. I have a simple boundary system setup where if the player's x position is higher than 0 he can move left. But the image I'm using for the character is clipping off the screen a bit. I know it's because the x position isn't exact, just wanted to know if there's a better way to do boundaries.
- CogentInvalid
- Prole
- Posts: 27
- Joined: Sat Dec 14, 2013 12:15 am
Re: What's the best way to make boundaries around the screen?
Presumably you're doing something like this:
The problem with this is that if the value of x is 2, for instance, 5 will be subtracted and the player will end up at a position of -3, outside the bounds of the screen.
The solution is to actively nudge the player back in-bounds if they go out of bounds, like this:
Code: Select all
if x > 0 and love.keyboard.isDown("left") then
x = x - 5
end
The solution is to actively nudge the player back in-bounds if they go out of bounds, like this:
Code: Select all
if love.keyboard.isDown("left") then
x = x - 5
end
if x < 0 then
x = 0
end
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: What's the best way to make boundaries around the screen?
Or assuming you want to minimize unnecessary branches like that,
Code: Select all
if love.keyboard.isDown("left") then
x = math.max(x - 5, 0)
end
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: What's the best way to make boundaries around the screen?
These work perfectly for what I am trying to do. Thanks for the responses!
Re: What's the best way to make boundaries around the screen?
function math.clamp(a,low,high)
math.max(low,math.min(a,high))
end
so clamp your position within the sceen size.
math.max(low,math.min(a,high))
end
so clamp your position within the sceen size.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests