Defold rendering issue (SOLVED)

I am going to make simple 2D game in Defold game engine and I am new in this area. My question is, I have 2 game objects every object has sprites in it. A sprite in the first game object must be background for sprites of second object. I have designed it well but when I run (or render, I don’t know how to call properly) it sometimes sprites of second game object are invisible and some times everything is OK.


The same issue if I set main backgruon image for the game. Please share your experiences with me. Thanks beforehand.

You need to use different Z (depth) positions on the objects so one is rendered in front of the other.

The Z position should be between -1 and 1. 1 is most on top and -1 is furthest back.

3 Likes

Thank you very much. I have tried the Z position but from 1 to 10 not as you said from -1 to 1. Thanks again :slight_smile:

The default render script (found in builtins/render/ and referenced from game.project) sets a z-range of -1 to 1. You can make a copy of the render script and reference it in your project and set a totally different range if you like (I usually go for something like -10 to 10).

2 Likes

One more thing to note: placing things at the same Z position will generally cause unwanted behavior (sometimes one is rendered in front of the other, and sometimes the other way around, which I believe is due to floating point errors). For that reason, it is usually good to place all objects that might conceivably intersect at some point at entirely different Z values.

This can even be done discretely, in line with what @britzl mentions, in a more traditional layer based approach, where a background layer has Z value -10, the next layer -9, etc. all the way up to layer 20. This could make some issues related to layers become very easy to spot and fix.

6 Likes