Moving sprite leaves traces

Hey,

I've got two sprites:

  • the wall that uses draw commands to just draw an outline
  • the ball that is also just a draw command to fill a circle

The ball has a vector and in its update method I just move it by this vector. What I can't figure out is why the ball is leaving traces like this? I've tried filling the rect of the frame sprite to clear it out, but that doesn't seem to work. I feel like I'm missing something about the sprite/drawing system?

Here's a gist of the entire code (general pointers welcome, although this is just a super quick mockup)

Thanks

The (x,y,w,h) parameters passed to the sprite callback give the dirty rect (in sprite-local coordinates with top-left origin) that needs redrawn. That trail happens because the World sprite is drawing its border inside the update rects as the ball moves instead of in the full sprite rect. You could change the code to always draw the border at full size, or you can use the dirty rect params to avoid drawing the border if it's not needed and save a bit of CPU:

function World:draw(x, y, w, h)
  gfx.setColor(gfx.kColorWhite)
  gfx.fillRect(x, y, w, h)
  gfx.setColor(gfx.kColorBlack)

  local selfw, selfh = self:getSize()

  if x <= 2 or x+w >= selfw-4 or y <= 2 or y+h >= selfh-4 then
    gfx.setStrokeLocation(gfx.kStrokeInside)
    gfx.drawRoundRect(0, 0, selfw, selfh, 2)
    gfx.drawRect(2, 2, selfw - 4, selfh - 4)
  end
end

Ah, of course. That makes perfect sense. And is clearly stated in the docs... :sweat_smile: sorry!

1 Like