Issue with Sprite draw() Function Not Being Called

Hello everyone,

I'm currently developing a game for the Playdate using Lua, and I'm facing an issue with the sprite draw() function not being called.

Here is a brief description of the problem:

  • I have a Item class that extends gfx.sprite.
  • The Item:update() function is being called repeatedly, and the coin's position is updating as expected.
  • However, the Item:draw() function is not being called, and therefore the blinking effect is not working.

I have tried the following solutions:

  • Ensured that the Item sprite is added to the sprite system using self:add().
  • Set self:setVisible(true) and self:setUpdatesEnabled(true).
  • Used self:setIgnoresDrawOffset(true) to ensure that the draw function is called even if the sprite is off-screen.
  • Added debug messages inside the draw() function to check if it is being called, but no messages are appearing.

Here is a snippet of my Item class for reference:

lua

class('Item').extends(gfx.sprite)

function Item:init(game, x, y, itemType, groundY)
    Item.super.init(self)
    -- Initialization code...
    
    -- Set the image
    self.image = self.imageTable:getImage(1)
    self:setImage(self.image)
    
    -- Configure the sprite
    self:setSize(imageWidth, imageHeight)
    self:setCenter(0.5, 0.5)
    self:setZIndex(50)
    self:setUpdatesEnabled(true)
    self:setVisible(true)
    self:setIgnoresDrawOffset(true)
    self:add()
    
    print("Item:init - Added to sprite system")
end

function Item:draw()
    print("Item:draw() called at position:", self.x, self.y)
    self.image:draw(0, 0)
end

I would greatly appreciate any advice or suggestions on how to resolve this issue. Thank you in advance for your help!

Best regards, [Jbuta]

I think this is because you're also setting an image for the sprite, but the Inside Playdate docs for sprite:draw() state it's one or the other:

If the sprite doesn’t have an image, the sprite’s draw function is called as needed to update the display.
[...]
Note that the callback is only called when the sprite is on screen and has a size specified via sprite:setSize() or sprite:setBounds().

So to use a custom draw() function for a sprite you should not set an image but still set a size in one of those ways, otherwise the sprite will draw the set image via its internal logic, and you can control what's displayed via the other sprite functions (switching to other images, changing visibility, etc.).

2 Likes

Hello everyone,

I'm thrilled to share that the issue with the sprite draw() function has been completely resolved!

Your advice and insights, especially regarding the use of setImage() within the updateAnimation() function, were crucial in identifying and solving the problem. Thanks to your guidance, the blinking effect for the coin is now working perfectly.

I sincerely appreciate the support and expertise of this community. Thank you all for your invaluable help!

Best regards, [Jbuta]

1 Like