Sprites not being removed

I'm on Windows 10.

I have functions that:

  • draw a large box
  • draw text in the large box
    then on A press,
  • it's supposed to clear the sprites this is what's not working
  • display two other boxes with text

I have a function that has multiple ways of removing sprites, and it runs, but the... sprites aren't being removed? Only if I clear the whole screen using gfx.clear will anything happen.

I have a scene manager that runs the clearing function ("tidy") every tick (when A is pressed). It's just not removing the sprites.

-- function to clear it all up:
 
function Dx5:tidy()
    print("tidy...")
self.spritedx:remove()
self.spritenvlbox:remove()
playdate.graphics.sprite.removeSprite(self.spritedx)
playdate.graphics.sprite:removeAll()
--none of this is working!!
    if self.kind ~= "nvl" then
        self.spritenamebox:remove()
        playdate.graphics.sprite.removeSprite(self.spritenamebox)
    end
end
 
 
 
--how it's being called:
 
tick = function()
        if playdate.buttonJustPressed(playdate.kButtonA) then
           print("a press")
         --  gfx.clear() -- this works but tidy does not...
        Dx5:tidy() --so this should remove em all...
 
        local dx ="111new math"
            Dx5:Pr1(dx) -- this is sending it to the first prefixinator
          print("ran dx5 with", dx)
        end
    end,
 
-- the scene manager, for reference:
 
 
scene["gallery"] = {
    start = function ()
       --beginning of scene
end,
     tick = function()
    --this runs every frame
 
--this is where the tidy function is being called; so it should be running a LOT
 
end,
    close = function()
       --when you close
end
}
 
--PD update:
 
function playdate.update()
    gfx.sprite.update()
    scene[current_scene]["tick"]()
    playdate.timer.updateTimers()
end
 
-- and how my sprites are set up:
 
 
 
 gfx.pushContext(namebox)
   namebox:drawInRect(0,self.height+nbHeight,nbWidth,nbHeight)
   gfx.drawTextInRect(name,10,self.y-15,self.maxWidth-5,25,nil,"...",kTextAlignment.left)
 gfx.popContext()
 self.spritenamebox = gfx.sprite.new(namebox)
 self.spritenamebox:moveTo(50,50) --50,50
 self.spritenamebox:setZIndex(65)
 self.spritenamebox:add()

-- pretty much this for like 5 sprites.
 

like... am i creating sprites wrong or...? I've had multiple people look and we couldn't figure it out, so I'm hoping yall can figure it out.

e: video!

So the back sprite should be removed when the other one comes into play, but it isn't. It just isn't.

Try putting:

print(“n sprites: “ .. gfx.sprite.spriteCount())

Before and after sprite:removeAll just to confirm it’s working. I suspect it is removing the sprites then regenerating them, but there’s not enough of your code here to work out why.

okay:
on first A-press, it says 2, we remove, it says 0. (despite the one still being visible) (the kind = nvl on this one)
On a second a-press, there's 3, we remove, it says 1, and it just goes up from there. (the kind ~= nvl on the rest)

also just realized my tidy code in OP isn't quite up to date --


function Dx5:tidy()
    print("tidy...")
    print("n sprites:" .. gfx.sprite.spriteCount())
    if self.kind == "nvl" then
self.spritenvldx:remove()
self.spritenvlbox:remove()
print("tidy nvl")
playdate.graphics.sprite:removeAll()
    elseif self.kind ~= "nvl" then
self.spritenamebox:remove()
self.spritedx:remove()
playdate.graphics.sprite.removeSprite(self.spritenamebox)
print("tidy reg")
end
print("n sprites:" .. gfx.sprite.spriteCount())

end

Okay, so you need to look at the conditions that are causing the sprites to be rebuilt. That’s the bit of your code that’s not shown here. If you can share a git or zip your source folder I’d be happy to take a look.

source.zip (8.0 MB)

thank you for your offer to help! it's a LOT of spaghetti and i'm a pretty novice programmer, but the main thing to look at should be dialogueSYSTEM.lua

Well, the good news is that sprites are being removed cleanly in the version you sent. The bad news is that they're not drawn until I press :a:.

I think the original issue was that scene[current_scene]["tick"]() in playdate.update was regenerating the sprites each frame.

1 Like

both of those are intended, at least for now. thanks for the help though

Figured it out. :person_facepalming:

The way I was using push/popcontext with a nineslice was drawing it as an image and not a sprite, so... it wasn't being removed because it wasn't a sprite...

OLD:

local nvl = nineslice
    gfx.pushContext(nvl)
nvl:drawInRect(self.x,self.nvly,390,self.nvlheight)
    gfx.popContext()
    --[[ --this... does nothing...........
    self.spritenvlbox= gfx.sprite.new(nvl)
    self.spritenvlbox:moveTo(0,150)
    self.spritenvlbox:setZIndex(60)
    self.spritenvlbox:add()
--]]

NEW:

local nvl = playdate.graphics.image.new(400, 240)
    nvl:drawCentered(200, 120)

    local nvl9 = nineslice
    gfx.pushContext(nvl)
      nvl9:drawInRect(self.x,self.nvly,390,self.nvlheight)
    gfx.popContext()
    
    self.spritenvlbox= gfx.sprite.new(nvl)
    self.spritenvlbox:moveTo(200,120)
    self.spritenvlbox:setZIndex(60)
    self.spritenvlbox:add()

TL;DR: i don't understand pushcontext.

1 Like