Академический Документы
Профессиональный Документы
Культура Документы
CORONA SDK
29 105 3
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
What You'll Be Creating
In this three-part series, I will be showing you how to create a game inspired by the
popular seventies game, Space Invaders. Along the way, you'll learn about Corona's
scene management functionality, timers, moving a character, the built-in physics
engine, and how to use modules to emulate classes in the Lua programming
language.
1. New Project
Open the Corona Simulator, click New Project, and configure the project as shown
below.
Select a location to save your project and clickOK. This will create a folder with a
number of icons and three files that are important to us, main.lua, config.lua, and
build.settings.
We'll take a look at each file in the next few steps.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
2. Build Settings
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
The build.settings file is responsible for the build time properties of the project.
Open this file, remove its contents, and populate it with the following configuration.
01 settings =
02 {
03 orientation =
04 {
05 default ="portrait",
06 supported =
07 {
08 "portrait"
09 },
10 },
11 }
3. Application Configuration
The config.lua file handles the application's configuration. As we did with
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
build.settings,
open this file, remove its contents, and add the following configuration.
01 application =
02 {
03 content =
04 {
05 width = 768,
06 height = 1024,
07 scale = "letterbox",
08 fps = 30,
09 }
10 }
This sets the default width and height of the screen, usesletterbox to scale the
images,
and sets the frame rate to 30. Visit the Corona documentation to learn more about
the other properties you can set in config.lua.
4. Entry Point
The main.lua file is the file that the application loads first and uses to bootstrap the
application. We will be using main.lua to set a few default settings for the
application and use the Composer library to load the first screen.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
If you're not familiar with Corona's Composer library, then I recommend giving the
documentation a quick read. In short, Composer is the built-in solution to scene
(screen) creation and management in Corona. The library provides developers with
an easy way to create and transition between individual scenes.
The newer Composer module replaces the older and now deprecated
StoryBoard module. A migration guide is available to help convert your old projects
over to use Composer.
1 display.setStatusBar(display.HiddenStatusBar)
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
To set the default anchor or registration points add the following code block to
main.lua.
1 display.setDefault("anchorX", 0.5)
2 display.setDefault("anchorY", 0.5)
The anchorX and anchorY properties specify where you want the registration point
of your display objects to be. Note that the value ranges from 0.0 to 1.0. For
example, if you'd want the registration point to be the top left of the display object,
then you'd set both properties to 0.0.
A good seed value is Lua's os.time function since it will be different each time the
application is run. Add the following code snippet to main.lua.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
1 math.randomseed( os.time() )
8. Avoiding Globals
When using Corona, and specifically the Lua programming language, one way to
have access to variables application-wide is to use global variables.The way you
declare a global variable is by leaving off the keyword local in front of the variable
declaration.
For example, the following code block declares two variables. The first one is a
local variable that would only be available in the code block it is defined in. The
second one is a global variable that is available anywhere in the application.
It is generally considered bad practice to use global variables. The most prevalent
reason is to avoid naming conflicts, that is, having two variables with the same
name. We can solve this problem by using modules. Create a new Lua file, name
it gamedata.lua, and add the following code to it.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
1 M = {}
2 return M
We simply create a table and return it. To utilize this, we use Lua's require
method. Add the following to main.lua.
We can then add keys to gameData , which will be the faux global variables. Take a
look at the following example.
Whenever we want to access these variables, all we have to do is use the require
function to load gamedata.lua. Every time you load a module using Lua's require
function, it adds the module to a package.loaded table. If you load a module, the
package.loaded table is checked first to see if the module is already loaded. If it is,
then it uses the cached module instead of loading it again.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
9. Require Composer
Before we can use the Composer module, we must first require it. Add the following
to main.lua.
1 composer.gotoScene("start" )
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
composer file, which means we need to require the Composer module and create a
composer scene. Add the following snippet to start.lua.
The call to newScene makes start.lua part of composer's scene hierarchy. This
means that it becomes a screen within the game, which we can call composer
methods on.
From here on out, the code added tostart.lua should be placed above the return
statement.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
It's important to understand that local variables in the main chunk only get called
once,
when the scene is loaded for the first time. When navigating through the composer
scenes, for example, by invoking methods like gotoScence , the local variables will
already be initialized.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
earlier,
you will have noticed the documentation includes a template that contains every
possible
composer event. The comments are very useful as they indicate which events to
leverage for initializing assets, timers, etc. We are interested in the scene:create,
scene:show , and scene:hide methods for this tutorial.
Step 1: scene:create
This method is called when the scene's view doesn't exist yet. This is where you
should initialize the display objects and add them to the scene. The group variable
is pointing to self.view , which is a GroupObject for the entire scene.
Composer's scene:show method has two phases. The will phase is called when
the scene is still off-screen, but is about to come on-screen. The did phase is called
when the scene is on-screen. This is where you want to add code to make the
scene come alive, start timers, add event listeners, play audio, etc.
In this tutorial we are only interested in thedid phase. Add the following code
snippet to start.lua.
We declare a local variable phase , which we use to check which phase the show
method is in. Since we will be coming back to this scene later in the game, we
check to see if there is a previous scene and, if so, remove it. We add a tap listener
to the startButton that calls the startGame function.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 3: scene:hide
Composer's scene:hide method also has two phases. The will phase is called
when the scene is on-screen, but is about to go off-screen. Here you will want to
stop any timers, remove event listeners, stop audio, etc. The did phase is called
once the scene has gone off-screen.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
the gamelevel scene.
1 function startGame()
2 composer.gotoScene(
"gamelevel")
3 end
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
1 scene:addEventListener("create", scene )
2 scene:addEventListener("show", scene )
3 scene:addEventListener("hide", scene )
Advertisement
Advertisement
Translations Available:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Build A Poker Game in Corona:
Interface Setup
Code
Jobs
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Advertisement
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Follow Us Email Newsletters
FAQ
Subscribe
Terms of Use
Contact Support Privacy Policy
About Tuts+
Advertise
Teach at Tuts+
Build anything from social networks to file upload systems. Build faster
with pre-coded PHP scripts.
Browse PHP on CodeCanyon
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
2014 Envato Pty Ltd. Trademarks and brands are the property of their respective
owners.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com