1. Computing

Corona SDK: How to Layer Graphics, Move Graphics and Bring Graphics to Front

Using the Corona SDK to Manipulate Graphics

By

A main component of AnscaMobile's Corona SDK is the display object, which can not only be used to draw graphics to the screen, but also to arrange those graphics in groups that can be easily manipulated to layer graphics, move graphic objects around the screen and bring a layer of graphics to the front.

This tutorial will teach you the basics of using display groups to organize the graphical objects in your project. This will be demonstrated by creating two different layers, one representing the normal screen and another representing a modal layer to placed on top of it. In addition to layering the graphics, we'll also use the transition object to move the entire modal group.

How to Market Your App

Note: In order to follow along with this tutorial, you will need a background image, a button image and the "ui.lua" class available from the enhanced UI library. You can get the button image from the button sample of code that comes with the Corona SDK. The ui.lua file can be downloaded here and the background image can be anything you choose. These files must be in the same directory as the one you use to build this tutorial.

To get started, we'll open a new file called main.lua and begin building our code:


local ui = require("ui");

displayMain=display.newGroup();
displayScreen=display.newGroup();
displayModal=display.newGroup();

This section of code sets up our ui library and declares through display groups: displayMain, displayScreen and displayModal. The displayMain group will be used to hold all of the other display groups within the project, allowing us to layer the graphic groups within the same main group.


function setupScreen()
  displayMain:insert(displayScreen);
  displayMain:insert(displayModal);

  displayScreen:toFront();
  displayModal:toFront();
  
end

The setupScreen function demonstrates how to add display groups to the main display group. We also use the toFront() function to set up the different graphic layers, with the layer we want on top all of the time declared last.

In this example, it is not really needed to move the displayScreen to the front since it will default to being below the displayModal group, but it is good to get into the habit of explicitly layering each display group. Most projects will end up with more than two layers.


function screenLayer()
  local background=display.newImage("background.png",0,0);
  displayScreen:insert(background);
end

The screenLayer function simply displays our test background image and places it within the displayScreen group to show how the layers work.


function setupModal()	
	local bg = display.newRect( 0, 0, 320, 480 );
	bg:setFillColor( 128, 128, 128, 255);
	displayModal:insert(bg);
	
	local btn= ui.newButton {
		defaultSrc = "button.png" , defaultX = "200" , defaultY = "50",
		overSrc = "button.png" , overX = "200" , overY = "50",
		size=16, 
		onEvent = modal_event,
		id = "modal_exit" }
	btn.x=160;
	btn.y=440;
	btn:setText("Done");	
	displayModal:insert(btn);

	displayModal.x=displayModal.x-320;
	transition.to(displayModal,{time=200, x=(displayModal.x+320)});

end

The setupModal function writes a set of graphics to the screen and uses the transition.to function to animate the entire group sliding into place from off the screen. Notice how each graphical object is inserted into the displayModal group. Also note that buttons and other UI elements can be inserted into the group.


function dismissModal()
  for i=displayModal.numChildren,1,-1 do
    displayModal[i].parent:remove(displayModal[i]);
  end
end

The important thing to note about the dismissModal function is how we iterate through the displayModal group backwards. This allows us to cleanly remove each display item.

Think of this as having a group of numbers from one to five. If we remove one first, the number three is suddenly the second object. But if we remove five first, the number three remains the third object in the group.


function modal_event(event)
  local button=event.id;
	
  if (event.phase=='release') then
    if (button=='modal_exit') then
      dismissModal();
    end
  end
end

We'll use the modal_event function to call the dismissModal function and remove the objects within the displayModal group.


setupScreen();
setupModal();
timer.performWithDelay(500,screenLayer);

Now that we have our code in place, we'll call the functions to set everything in motion. The setupScreen() puts our display groups within a main group and sets up the layers.

We call our setupModal() function next to demonstrate how this group of graphics will remain on top even when other graphics are being displayed later.

Last, we call screenLayer with timer.performWithDelay, ensuring the main screen graphics are called well after setupModal runs.

Without creating the display groups and using the toFront() function to create layers, the image used within the screenLayer function would pop up on top of our modal screen. But because of the layers, the modal group remains on top until dismissed.

This functionality will work with multiple layers and the display groups can be moved directly by setting the x and y variables or moved through the transition.to function. You can also use the toFront() function at any time to bring a group to the top layer.

©2014 About.com. All rights reserved.