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]
[import]uid: 52491 topic_id: 21007 reply_id: 83093[/import]