1
votes

I'm trying to create a circular shadow underneath a sprite image in my game. However, for some reason, even though I create the circle first, the sprite always appears underneath it. Here is the code:

local circle = display.newCircle(100,100,30)
circle:setFillColor(0,0,0)
local hunter = display.newSprite(imageSheet,createAnimationSequence("Hunter",5))

the createAnimationSequence function just organizes the animation sequences, so this cannot be the problem. I've tried putting both the sprite and the circle in its own display group, and even that did not work. I'm very confused by this issue, but it could just be a careless error I am not seeing.

The circle appears and so does the sprite, only the circle is on top of the sprite instead of underneath it, even though I code for it to be drawn first.

2

2 Answers

6
votes

In Corona SDK you can't change the z-index directly, there's a work around it.

You can use object:toBack() and object:toFront()

local circle = display.newCircle(100,100,30)
circle:setFillColor(0,0,0)
local hunter = display.newSprite(imageSheet,createAnimationSequence("Hunter",5))

circle:toFront()
hunter:toFront()

But be careful on object:toBack(), it may cause the object to move behind the background (if any), it is prefer to use it on a display.newGroup()

7
votes

you can directly put your circle behind the group using index

local group = display.newGroup()

local circle = display.newCircle(100,100,30)
circle:setFillColor(0,0,0)
local hunter = display.newSprite(imageSheet,createAnimationSequence("Hunter",5))

group:insert(1,circle)
group:insert(2,hunter)