Can't draw sprites contained inside other sprites

This could be a simple question, but I have an custom sprite that has several properties inside, and one of the properties is another sprite. I'm using SDK 2.4.0 on an M1 Mac. I'm trying to draw a circle around a filled circle. Here's a chunk of the code:

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

function ParentSprite:init(startingX, startingY, radius)
  ParentSprite.super.init(self)
  
  local parentSpriteImage = gfx.image.new(radius * 3, radius * 3)
  gfx.pushContext(parentSpriteImage)
      gfx.fillCircleAtPoint(radius, radius, radius)
  gfx.popContext()

  self:setImage(parentSpriteImage)
  self:moveTo(startingX, startingY)
  
  local childSpriteImage = gfx.image.new(radius * 2 * 1.125)
  gfx.pushContext(childSprite)
      gfx.drawCircleAtPoint(radius, radius, radius * 1.125)
  gfx.popContext()
  
  self.childSprite = gfx.sprite.new()
  self.childSprite:setImage(childSpriteImage)
  self.childSprite:moveTo(startingX, startingY)

I'm able to create the child sprite and its properties, but calling the child sprite's add() (via parentSprite.childSprite:add()) method would not draw it on the screen. Moving the child sprite out of the parent and making it its own sprite class does work with the same code. Is there something I'm missing to be able to use it like this?

Thanks!

Here are some problems with the code you've shared:

  1. The height argument is missing when creating the child sprite image
  2. The child sprite is pushed onto the graphics context instead of the child sprite image

Thanks a lot! I had fixed the wrong context but I missed the missing height arg. It works like a charm now

1 Like