object hierarchy question

I’m prototyping a game for my daughter and I’m struggling with a display object hierarchy problem.

I have a field of stars and a princess. The stars move at different rates depending on their proximity to the player.

I would like the princess to be behind the largest, fastest moving stars but in front of the slower stars and I’m struggling with it.

Thanks for your help!

I’m pasting the code below:
[lua]physics = require ( “physics” )
display.setStatusBar( display.HiddenStatusBar )

– LOCAL DECLARATIONS

local lg = display.newGroup()
_H = display.contentHeight;
_W = display.contentWidth;

– PRINCESS

local princess = function ()
princess = display.newImageRect(“images/Character Princess Girl.png” , 107, 171)
princess:setReferencePoint(display.CenterReferencePoint);
princess.x = _W / 2
princess.y = (_H / 2 + 100)
lg:insert(princess)

end

– GRASS

local grass = function ()
grass = display.newImageRect(“images/Grass Block.png”, 107, 171)
grass:setReferencePoint(display.CenterReferencePoint);
c = (grass.width / 2)
grass.x = c
grass.y = (_H - c)
end

– STARS

local starTable = {}

function initStar()
local star1 = {}
star1.imgpath = “images/star1.png”;
star1.movementSpeed = 1000;
table.insert(starTable, star1);

local star2 = {}
star2.imgpath = “images/star2.png”;
star2.movementSpeed = 12000;
table.insert(starTable, star2);

local star3 = {}
star3.imgpath = “images/star3.png”;
star3.movementSpeed = 14000;
table.insert(starTable, star3);
end

function getRandomStar()
local temp = starTable[math.random(1, #starTable)]
local randomStar = display.newImage(temp.imgpath)
physics.addBody(randomStar, { isSensor = true } )
randomStar.myName = “star”
randomStar.movementSpeed = temp.movementSpeed;
randomStar.x = math.random(0,_W)
randomStar.y = _H + 50
randomStar.rotation = math.random(0,360)
starMove = transition.to(randomStar, {
time=randomStar.movementSpeed,
y=-45,
onComplete = function(self) self.parent:remove(self); self = nil; end
})
end

local gameInit = function ()
physics.start()
–grass()
princess()
initStar()
starTimer1 = timer.performWithDelay(1700,getRandomStar, 0)
starTimer2 = timer.performWithDelay(2300,getRandomStar, 0)
starTimer3 = timer.performWithDelay(2700,getRandomStar, 0)

end

– START GAME

gameInit ()[/lua] [import]uid: 10763 topic_id: 21007 reply_id: 321007[/import]

Try adding;

[lua]star1:toFront()[/lua]

on line 40.

Let me know if that does the trick for the largest, fastest star - if so you may consider using that and modifying it for the slower stars.

Peach :slight_smile: [import]uid: 52491 topic_id: 21007 reply_id: 83093[/import]

Hey Peach,
Thanks for the reply. but when I add star1:toFront() on line 40 I get the following error:

[bash]

Runtime error: …CoronaSDK/Current/Princess Project/Princess/main.lua:40: attempt to index global ‘star1’ (a nil value)
stack traceback:
[C]: ?
…CoronaSDK/Current/Princess Project/Princess/main.lua:40: in function ‘princess’
…CoronaSDK/Current/Princess Project/Princess/main.lua:102: in function ‘gameInit’
…CoronaSDK/Current/Princess Project/Princess/main.lua:108: in main chunk
[/bash] [import]uid: 10763 topic_id: 21007 reply_id: 83145[/import]

It looks like at line 40 star1 is not a display object, but just a table, so that may be why toFront() is throwing an error? Also, I only see “princess” being added to the displayGroup “lg”. Don’t the stars also need to be in lg to be ordered relative to the princess? [import]uid: 22076 topic_id: 21007 reply_id: 83168[/import]

Ah, reviewing the code you’d actually do randomStar:toFront() if the star image was the largest - good catch, Carbondale.

As to groups, the stars should go in the localGroup anyway but if I recall correctly toFront() should still work fine so long as the star is meant to be at the absolute front - if there is a HUD or something that you’d only want to put it to the front of the group.

Peach :slight_smile: [import]uid: 52491 topic_id: 21007 reply_id: 83272[/import]

Thanks Peach & Carbondale,

I added:
[lua]if temp.imgpath == “images/star1.png” then
randomStar:toFront()
end[/lua]
at line 91 and all works!

I’ll work on getting the groups sorted next.

Thanks again!

[import]uid: 10763 topic_id: 21007 reply_id: 83593[/import]

Excellent - glad to hear it :slight_smile:

Peach [import]uid: 52491 topic_id: 21007 reply_id: 83708[/import]

Awesome. I’m sure your daughter is going to love her custom-made game! I’ve made a couple of fairly simple apps with my grandson in mind, and it’s very satisfying to see him enjoying them.
[import]uid: 22076 topic_id: 21007 reply_id: 83781[/import]