Run-time changing layer property (SOLVED)


(christopf) #1

My question (maybe you have read it in the other thread) is if its possibly to make the layer property change in relation to the enitites position on the map? Right now im working on a 3/4 topdown game and would like to have my character hidden when he walks behind a stone entity. but when he walks around and in front of the stone he should be visible. for now i had a solution to split the entity up in 2 parts - a hat and a foot. the hat is the top layer. after this comes the player and then the foot part of the stone. the player entity isnt high enough to grow over the foot part so he cant get hidden by the hat. but this is kinda no so elegant solution and will be more difficult when i add objects and maybe moving enemy entities.

I was thinking, this should be made easy if each entitys layer is oriented at their y value. like this

entitiesName.layer = -entitiesName.y

but this aint working. you have any ideas on this?


(Elias) #2

I had a similar problem myself with a project of mine. I kinda though about a solution but i never actually put it in code. Maybe if the player collides and his Y position is bigger than the obstacle, his layer will be behind. If He collides and his Y is lower (than a certain point), he will apear in front of it. If somebody worked on something similar, i would like to know his solution as well.


(Jacob Albano) #3

Try setting the player’s origin to his feet. Your method of setting the layer based on y should be fine.


(christopf) #4

did it, but it still aint working. i thought its maybe because of the .oel is giving the x,y coordinates. but changing this didnt change the result.


(lordmagnet) #5

I use this same method for my game and it worked like a charm:

char.layer = -char.y; is exactly what I have.

Just put it in the update override of my game world.


(christopf) #6

Jay again. this was it, the update function. i feel like i aint learning…meow. Why is that logic not getting in my head?

For understanding: the setOrigin function has to be in die constructor because it aligns along the x and y value right? But this isnt the green mini rectangle in the console is it? Because the layer property orientates itself at this rectangle, i guess i can work around with something like -(this.y+30) or kinda thing. but it would be nice to know the exact behavior or it.


(Jacob Albano) #7

You don’t need to set the origin in the constructor, and yes, the green rectangle in the console does refer to the entity’s origin.

My suggestion was to set your player’s graphic’s origin to its bottom, so everything will display correctly.

graphic = myImage;
myImage.originX = myImage.width / 2;
myImage.originY = myImage.height.

(christopf) #8

thank you. you saved me again jacob. i guess i should begin to gather my cake recipes…


(Ultima2876) #9

This topic was automatically closed after 2 days. New replies are no longer allowed.