1

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.

Jordan Brown
  • 638
  • 1
  • 5
  • 14

2 Answers2

7

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)
DevfaR
  • 1,586
  • 1
  • 9
  • 19
  • your answer worked, but i felt the work around the other answer gave (without creating a new group) is more what i was hoping for. either way-- thanks so much for the help! +1 – Jordan Brown Jul 08 '13 at 02:17
6

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()

NaviRamyle
  • 3,967
  • 1
  • 31
  • 49