how to determine if sprite is on screen?

how would you determine if a given sprite is on screen or not?

Here are two solutions that both assume the anchorX/anchoY of the object is 0.5.

The optimistic version calculates a radius about the object center and assumes it is visible (onscreen) if the object position and radius touch or are within the edge of the screen.

-- True edges of screen (regardless of scaling mode in config.lua) local left = 0-(display.actualContentWidth - display.contentWidth)/2 local top = 0-(display.actualContentHeight - display.contentHeight)/2 local right = display.contentWidth + (display.actualContentWidth - display.contentWidth)/2 local bottom = display.contentHeight + (display.actualContentHeight - display.contentHeight)/2 -- Following functions assume anchorX/anchorY == 0.5 (i.e. center) local function isOnScreenByCenter( obj, buffer ) buffer = buffer or 0 if( (obj.x + buffer) \> right ) then return false end if( (obj.x - buffer) \< left) then return false end if( (obj.y + buffer) \> bottom ) then return false end if( (obj.y - buffer) \< top ) then return false end return true end local function isOnScreenOptimistic( obj ) -- Works even for rotated local maxRadius = (obj.contentWidth \> obj.contentHeight) and obj.contentWidth or obj.contentHeight maxRadius = (maxRadius / 2) \* math.sqrt(2) maxRadius = math.floor( (maxRadius \* 10^2) + 0.5) / (10^2) if( (obj.x - maxRadius) \> right ) then return false end if( (obj.x + maxRadius) \< left) then return false end if( (obj.y + maxRadius) \< top ) then return false end if( (obj.y - maxRadius) \> bottom ) then return false end return true end

Usage:

local tmp = display.newCircle( left, 100, 10) print( "Is onscreen? ", isOnScreenOptimistic( tmp ) )

Thanks!

Here are two solutions that both assume the anchorX/anchoY of the object is 0.5.

The optimistic version calculates a radius about the object center and assumes it is visible (onscreen) if the object position and radius touch or are within the edge of the screen.

-- True edges of screen (regardless of scaling mode in config.lua) local left = 0-(display.actualContentWidth - display.contentWidth)/2 local top = 0-(display.actualContentHeight - display.contentHeight)/2 local right = display.contentWidth + (display.actualContentWidth - display.contentWidth)/2 local bottom = display.contentHeight + (display.actualContentHeight - display.contentHeight)/2 -- Following functions assume anchorX/anchorY == 0.5 (i.e. center) local function isOnScreenByCenter( obj, buffer ) buffer = buffer or 0 if( (obj.x + buffer) \> right ) then return false end if( (obj.x - buffer) \< left) then return false end if( (obj.y + buffer) \> bottom ) then return false end if( (obj.y - buffer) \< top ) then return false end return true end local function isOnScreenOptimistic( obj ) -- Works even for rotated local maxRadius = (obj.contentWidth \> obj.contentHeight) and obj.contentWidth or obj.contentHeight maxRadius = (maxRadius / 2) \* math.sqrt(2) maxRadius = math.floor( (maxRadius \* 10^2) + 0.5) / (10^2) if( (obj.x - maxRadius) \> right ) then return false end if( (obj.x + maxRadius) \< left) then return false end if( (obj.y + maxRadius) \< top ) then return false end if( (obj.y - maxRadius) \> bottom ) then return false end return true end

Usage:

local tmp = display.newCircle( left, 100, 10) print( "Is onscreen? ", isOnScreenOptimistic( tmp ) )

Thanks!