We're finally getting into the visually appealing part of this chapter by starting to add in display objects using images. We don't have to refer to the terminal window for now. So, let's focus on the simulator screen. We'll begin by creating a background image and some art assets by performing the following steps:
Display Objects
.Chapter 2 Resources
folder, copy the glassbg.png
and moon.png
image files and the config.lua
file into your Display Objects
project folder.main.lua
file for your current project.local centerX = display.contentCenterX local centerY = display.contentCenterY local background = display.newImage( "glassbg.png", centerX, centerY, true) local image01 = display.newImage( "moon.png", 160, 80 ) local image02 = display.newImage( "moon.png" ) image02.x = 160; image02.y = 200 image03 = display.newImage( "moon.png" ) image03.x = 160; image03.y = 320
The background display object should contain the filename of the background image in your project folder. For example, if the background image filename is called glassbg.png
, then you would display the image like so:
local background = display.newImage( "glassbg.png", centerX, centerY, true)
Using image02.x = 160; image02.y = 200
is the same as the following lines of code:
image02.x = 160 image02.y = 200
The semicolon (;
) indicates the end of a statement and is optional. It makes it easier to separate two or more statements in one line and saves adding extra lines in your code.
The display objects for the image01
, image02
, and image03
variables should contain the moon.png
filename. The filenames in your code are case sensitive, so make sure that you write it exactly how it displays in your project folder.
The placement of image02
and image03
are specified from the local origin of the display object and positioned by the local values of the x and y properties of the device screen. The local origin is at the center of the image; the reference point is initialized to this point. Since we didn't apply (left, top) values to image02
and image03
, further access to x or y properties are referred to the center of the image.