How would you implement layers?Monkey Forums/Monkey Beginners/How would you implement layers?
| Hi everyone,|
I'm really new to Monkey, and going through the invaderjim's videos which I find fantastic! I'm an artist(animator) with a passion for coding, so I always try to organize my code with the visuals in mind.
I'd love to create a few helper classes to create a sort of layering system, where I can add objects that would react to these layers.
From what I got so far using the push/pop matrix is the way to go, so I can have a master layer that controls the "stage" with resolution indipendence, etc and then a list of layers that gets rendered within this master layer to each of which I associate all the objects that I want.
Am I on the right track with this, or is there a different/simpler method I could use?
Thanks a lot.
| If by layers you mean the Z-Order of rendering (place some images on top of others) I think your best option is to organize rendering order. You could create a class "stage" that has a collection of "layers" and each layer has a "render" method. Then call the "render" of each member of the "stage" class in the list order. That would be like rendering from back to front of a Z-Order. |
| Thanks, that's exactly what I'm trying to achieve as well as some helper functions to create layers and set them up. |
| This is a very very basic example on how this could be implemented. You can take it as a starting point to develop your own, if you like it:|
Notice that the 2 sample layers are just a square and a moving circle, but a layer could be whatever. I mean, a single layer could be used to draw lots of elements, no need to create a layer per element.
| Thanks a lot. It is what I'm doing at the moment (more or less). It's fun getting into these things :) |
| On a side note, what if I want to transform these layers for parallax effects for example?Is this where the pop/push matrix come into play? |
| push/pop matrix is used for graphics manipulation such as scale, rotate and translate. It may be useful for what you are doing but not essential and not necessarily helpful. |
if you want to scale an image use the push matrix and pop matrix on that specific image so the rest of the graphics do not get affected by the scaling. scaling and rotation work the same way.