How to shift an object's layer?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
LuaMaster
Prole
Posts: 13
Joined: Tue Jan 01, 2013 2:33 am

How to shift an object's layer?

Post by LuaMaster »

Hello.
I have a question. How would I make object x be infront of object y, besides drawing x first? Like, if I wanted to change it later, what function would I use.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: How to shift an object's layer?

Post by Helvecta »

Drawing order is pretty much the only way to put one thing in front of another. I suggest making a depth array, with each entry in the array representing a drawable object. Then in a drawing function you would establish a for loop to draw each object in the array from last to first so that the first in the depth array is drawn last (therefore drawn on top).

With that in place you could just make depth[1] the object you want in front of depth[2], depth[3], etc.

I'd write a simple example for you but my hand's sliced up right now and it's a real pain programming when you're typing 30WPM. :cry:

EDIT: Went ahead and wrote a sample for you, it features a slightly different but equally efficient way of doing layers. Use up and down keys to change layers. Sorry I didn't comment on it, my hand .. :death:
Attachments
depth.love
(260.2 KiB) Downloaded 139 times
"Bump." -CMFIend420
LuaMaster
Prole
Posts: 13
Joined: Tue Jan 01, 2013 2:33 am

Re: How to shift an object's layer?

Post by LuaMaster »

Wow thanks, this actually solves ANOTHER problem I was having, along with the ZIndex! From a glance at your code, you're telling what is going to be drawn outside of love.draw, AND with a ZIndex
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: How to shift an object's layer?

Post by Helvecta »

Yep yep! :nyu: Since the zIndex of each picture could change from frame to frame I stuck it in the love.update function; it's a pretty good idea to keep code in its relevant function (drawing stuff stays in love.draw, things like variables that can change from frame to frame stays in love.update, etc).
"Bump." -CMFIend420
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests