Is there a convenient way to determine whether a sprite is in the display list (that is, add()
has been called, and remove()
hasn't been subsequently called) short of looping through the list of all sprites in attempt to find it?
The SDK provides a way to do this here.
playdate.graphics.sprite.getAllSprites()
Edit: I forgot to add that this returns an array. You could copy the list and then iterate through it to see if any of its values match something you want to find.
Sorry, I might not have been clear. I'm aware of how to do this by looping through the list returned by that function. I'm asking if that's the only way to do it, or if there's something more convenient, along the lines of mySprite.added
or mySprite:isDisplayed()
, etc.
playdate.graphics.sprite:isVisible()
?
Invisible sprites still remain in the display list, so that doesn’t provide the info I’m looking for.
Looks like we don't currently have a function that can tell you that; I've filed a feature request, will be easy to add. In the mean time you could override the current add/remove functions to add that ( untested code follows):
local spritelookup = {}
local _spriteadd = playdate.graphics.sprite.add
local _spriteremove = playdate.graphics.sprite.remove
playdate.graphics.sprite.add = function(s)
_spriteadd(s)
spritelookup[s] = true
end
playdate.graphics.sprite.remove = function(s)
_spriteremove(s)
spritelookup[s] = nil
end
playdate.graphics.sprite:wasAdded = function(s)
return spritelookup[s] ~= nil
end