Hello everyone!
I'm trying to understand the following:
I see that in the documentation, the sprite's draw callback will be provided with a dirty rect (x, y, w, h)
. Where x
and y
are usually 0, 0 and w
, and h
are the size of the sprite. This makes sense to me since the whole sprite is being marked dirty and will be redrawn.
However, once you setDrawOffset
the x and y coordinates shift over with the draw offset. This causes an image drawn within a sprite at x
and y
to shift over.
After looking through the forums, I found this thread: setDrawOffset and Sprite:draw that mentioned this behavior but there was no further discussion. Neither image:drawIgnoringOffset
or sprite:setIgnoresDrawOffset
help in this case.
I could work around this in many ways:
- passing (0,0) to
image:draw
- using
sprite:setImage()
instead of implementing my ownsprite:draw()
My main questions is:
Is the dirty rect passed to Sprite:draw supposed to shift over with draw offset?
I'm on Windows SDK 2.4.1.
Here's a small example of the described scenario:
local image = playdate.graphics.image.new("assets/img")
local mySprite = playdate.graphics.sprite.new()
mySprite:moveTo(200, 120)
mySprite:setSize(32, 32)
mySprite:setCollideRect(0, 0, 32, 32)
mySprite:add()
function mySprite:draw(x, y, width, height)
-- x and y here are 0,0 until the offset kicks in.
image:draw(x,y)
end
function playdate.update()
playdate.graphics.sprite.update()
if playdate.buttonIsPressed( playdate.kButtonRight ) then
local nx = mySprite.x + 1
mySprite:moveTo(nx, mySprite.y)
end
if playdate.buttonJustPressed( playdate.kButtonA ) then
mySprite:moveTo(200, 120)
end
local xOffset = 0
local yOffset = 0
if mySprite.x > (250) then
xOffset = mySprite.x * -1 + (250)
end
playdate.graphics.setDrawOffset(xOffset, yOffset)
mySprite:markDirty()
end