Introduction:
Corona is a popular framework that can be used to rapidly develop cross platform mobile apps. Apps are developed using the Corona SDK which is based on the Lua scripting language. Corona has a vibrant community of developers creating apps which has led to many great tutorials about using Corona available already. This blog takes a specific focus on Android gaming and shows how to get started developing some basic components for a game and deploying it onto an Intel® Android* x86 device. In particular, displaying graphics, using the physics engine, detecting collisions, handling gestures, playing sounds, user interface widgets, saving & restoring state, and building & launching the Android APK will be discussed.
Development Environment:
The recommended development environment is using the Corona Editor. This editor is a combination of using the Sublime Text 2 Editor with a Corona Plugin. This plugin allows running, debugging, setting breakpoints, and single stepping through code. This is a nice development environment for developing with the Corona Simulator.
To learn more and get started with the Corona Editor, please see the link below.
http://coronalabs.com/products/editor/
Displaying Images:
Displaying an image in corona can be accomplished in 1 line of code. The image file name and screen coordinates are passed to the function.
local myBall = display.newImage("myBall.png", display.contentCenterX, 20)
To learn more about the display API's, please see the link below.
http://docs.coronalabs.com/api/library/display/index.html
Using The Physics Engine:
Corona comes with a powerful physics engine that is easy to use. In this example, a ball is put into motion that can bounce off each side of the screen.
-- 1. Add required code for using the physics engine local physics = require("physics") physics.start() -- 2. Add bumper images to each side of the screen local topBumper = display.newImage("top.png", display.contentCenterX, 0) local bottomBumper = display.newImage("bottom.png", display.contentCenterX, display.contentHeight) local leftBumper = display.newImage("left.png", 20, display.contentCenterY) local rightBumper = display.newImage("right.png", display.contentWidth-20, display.contentCenterY) -- 3. Assign a body with density, friction, and bounce characteristics to each bumper image physics.addBody( topBumper, "static", {density=3.0, friction=0.5, bounce=0.6} ) physics.addBody( bottomBumper, "static", {density=3.0, friction=0.5, bounce=0.6} ) physics.addBody( leftBumper, "static", {density=3.0, friction=0.5, bounce=0.6} ) physics.addBody( rightBumper, "static", {density=3.0, friction=0.5, bounce=0.6} ) -- 4. Add the ball image to the screen local myBall = display.newImage("myBall.png", display.contentCenterX, 20) -- 5. Assign a body with density, friction, and bounce characteristics to the ball physics.addBody( myBall, {density=3.0, friction=0.5, bounce=1.0} ) -- 6. Set the gravity initial value local gravity=4 physics.setGravity(0, gravity)
To learn more about the physics API's, please see the link below:
http://docs.coronalabs.com/api/library/physics/index.html#physics.
Detecting Collisions:
Corona makes it easy to detect object collisions. In this example, when the ball collides with one of the bumpers, it is detected and a sound is played.
--1. Implement the onBallCollision listener and play a sound when the collision begins local function onBallCollision ( event ) if ( event.phase == "began" ) then media.playSound( "bounce.mp3" ) end end --2. Add runtime listener for the collision event Runtime:addEventListener( "collision", onBallCollision )
To learn more about collision detection and media API's, please see the links below:
http://docs.coronalabs.com/guide/physics/collisionDetection/index.html
http://docs.coronalabs.com/api/library/media/index.html
Handling Gestures:
Corona makes it easy to handle touch gestures. In this example, a swipe up with a finger is used to increase the gravity and a swipe down with a finger is used to decrease the gravity.
--1. Implement the onBallTouch listener and modify the gravity when an up or down finger swipe is detected on the ball local function onBallTouch( event ) if ( event.phase == "moved" ) then if ( event.y > event.yStart) then gravity = gravity+1 else gravity = gravity-1 end physics.setGravity(0,gravity) print(gravity) end end --2. Add an event listener for when the ball is touched myBall:addEventListener( "touch", onBallTouch )
To learn more about the touch API's, please see the link below:
http://docs.coronalabs.com/api/event/touch/index.html
User Interface Widgets:
Corona provides a rich collection of familiar widgets such as buttons, pickers, and sliders for building user interfaces. In this example, a slider is used to change the gravity of the scene so the ball falls at a different rate.
-- 1. Add required code to use widgets local widget = require( "widget" ) -- 2. Implement the slider listener and modify the gravity local function mySliderListener( event ) gravity = gravity*(event.value/100) physics.setGravity( 0, gravity ) print(gravity) end -- 3. Declare the slider and provide the coordinates, width, initial value, and listener local mySlider = widget.newSlider { left = display.contentCenterX-80, top = 0, width = 150, value = 50, listener = mySliderListener }
To learn more about the widget API's, please see the link below:
http://docs.coronalabs.com/api/library/widget/index.html
Saving & Restoring State:
Corona provides file read & write capability so that app state can be saved and restored for providing an optimal user experience. In this example, the gravity set from the slider is saved and restored.
-- Saving:
-- 1. Provide the file path local path = system.pathForFile( "myGameFile.txt", system.DocumentsDirectory ) -- 2. Open the file for writing local file = io.open( path, "w" ) -- 3. Write the gravity value to the file file:write( gravity ) -- 4. Close the file io.close( file ) file = nil -- Restoring: -- 1. Provide the file path local path = system.pathForFile( "myGameFile.txt", system.DocumentsDirectory ) -- 2. Open the file for reading local file = io.open( path, "r" ) -- 3. Read the gravity value from the file gravity = file:read( "*a" ); -- 4. Close the file io.close( file ) file = nil -- 5. Restore the gravity in the physics engine physics.setGravity(0,gravity)
To learn more about the io API's, please see the link below
http://docs.coronalabs.com/api/library/io/index.html
Building the Android APK:
Corona uses a cloud based build environment to build the lua code into an Android APK. To initiate a debug test build, simply click File->Build for Android and click the Build button.
To learn more about building and distributing Android APK's, please see the link below:
http://docs.coronalabs.com/guide/distribution/androidBuild/index.html
Launching the Android APK:
Launching the Android APK on an Intel® Android x86 device is easily accomplished by using the Android Debug Bridge(ADB). ADB is part of the Android SDK that can be downloaded from developer.android.com. To install the APK onto the device, issue the following command from the command line:
adb install -r <appName.apk>
To learn more about installation, please see the link below:
http://docs.coronalabs.com/guide/distribution/androidBuild/index.html#installapp
Additional Links:
http://coronalabs.com/resources/tutorials/getting-started-with-corona/
++This sample source code is released under the MIT License
Immagine icona:
Clik here to view.
