Hello again, LÖVErs.
In my little game I try to determine if some object (picture, enemy, what have you) has left the screen by checking to see if its "x" and "y" components are greater than the length and/or width. Is this a good way of telling this? How about for multiple objects? I would like to find a method for detecting whether they've gone off the screen, yet doesn't take up a whole lot of processing and resources.
Checking for going off the screen
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Checking for going off the screen
Booted, suited, and ready to get executed.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Checking for going off the screen
Yes.Ertain wrote:Is this a good way of telling this?
Use tables and the same method.Ertain wrote:How about for multiple objects?
Ertain wrote:I would like to find a method for detecting whether they've gone off the screen, yet doesn't take up a whole lot of processing and resources.
Code: Select all
function offscreen(object) -- assuming (x, y) is the top-left position of the object.
return object.x < -object.width or object.y < -object.height object.x > love.graphics.getWidth() or object.y > love.graphics.getHeight()
end
Help us help you: attach a .love.
Re: Checking for going off the screen
Try this:
to use it on multiple objects you would just do something like this:
Code: Select all
-- This function returns true if the object is out of the screen.
-- trans_x and trans_y must be what you feed to love.graphics.translate()
-- scale must be what you feed to love.graphics.scale()
-- If scale and translate are not used then you only have to provide the object
function out_of_screen(object, trans_x, trans_y, scale)
local scr_left = -trans_x or 0
local scr_top = -trans_y or 0
local scr_bottom = scr_top + love.graphics.getWidth() * (scale or 1)
local scr_right= scr_left + love.graphics.getWidth() * (scale or 1)
local obj_left = object.x
local obj_top = object.y
local obj_right = object.x + object.width
local obj_bottom = object.y + object.height
return obj_left > scr_right or obj_top > scr_bottom or obj_right < scr_left or obj_bottom < scr_top
Code: Select all
for key, value in pairs(objects)
if out_of_screen(value) do
--something
end
end
Re: Checking for going off the screen
Thanks for the input, guys.
Booted, suited, and ready to get executed.
Who is online
Users browsing this forum: No registered users and 3 guests