Once you’ve created more than one object the order in which they stack will be come important. What I mean by “stacking order” is which object is in front and which is behind, imagine layers in Photoshop.
Corona stacks or layers objects in the order that they are created. In other words the first object created would be on the bottom. While each newer object created would stack on top of the previous.
Try this out for yourself. Create a new Corona project.
- Make a new Folder to work in.
- Make a new text file, save it to the folder as main.lua.
- Add three small images to your folder.
Create three images in your Corona project. Use the following to load your images. In my example the images were named square.png, circle.png and hexagon.png.
local square = display.newImage("square.png") local circle = display.newImage("circle.png") local hexagon = display.newImage("hexagon.png")
Notice the square is behind the other images. The circle “stacks” or looks like it’s on a layer between the other two. And, the hexagon is on top of the others.
Note: If any of the upper are larger than the images below, the images may not be visible.
The stacking order is determined by the order the images were created. With newer images being placed on top of images that were created earlier.
Corona provides a few methods that allow you to manipulate the stacking order. To move an object to the front use object:toFron(). For example, the following would move the circle to the front.
circle:toFront()
Use the object:toBack() method to move an object to the back. For example, the following moves the hexagon to the back.
hexagon:toBack()