Thursday, 17 December 2015

Mobile App Template For Corona

I've written a few apps in my time coding. Only one of them, Dead Run, has made it to the Google Play store. One thing I do keep in common, is I use the Corona SDK. Corona is a cross platform mobile app development software. You write the code once, Corona converts it for each platform.

One thing that I find makes me procrastinate in starting a new app is all the setup. Okay it's not really much but its still all the boring shit I don't really want to be doing. I want to create my app. So recently, I decided to make myself a template. This mean if I'm in the mood to start an app I can just pop in and start building.

The first file a corona app needs is the build settings. Here is my template build.settings file.

build.settings


settings =
{
  orientation =
   {
    default = "landscapeRight",
    supported = {"landscapeRight"}
   },
  
   androidPermissions =
   {
       "android.permission.WRITE_EXTERNAL_STORAGE",
       "android.permission.INTERNET",
       "android.permission.READ_PHONE_STATE",
       "android.permission.ACCESS_NETWORK_STATE",
   },
   android =
   {
        usesPermissions =
        {
            "android.permission.INTERNET",
            "com.android.vending.BILLING",
            "com.android.vending.CHECK_LICENSE"
        },
   },
   plugins =
    {
        ["plugin.google.iap.v3"] =
        {
            publisherId = "com.coronalabs",
            supportedPlatforms = { android=true }
        },
    }
}


This has all the basics I will need for my apps. If I find my app will need other settings I can easily return and add them in or even take some out later.

The next required file is..

config.lua


application =
{
    showRuntimeErrors = true,
   
    content =
    {
        graphicsCompatibility = 1,
        width = 720,
        height = 1080,
        scale = "zoomStretch",
        xAlign = "center",
        yAlign = "center"
    },
    -- license =
    -- {
        -- google =
        -- {
            -- key = "",
        -- },
    -- }

}


These two are probably the main two require file but I like to also set myself up a little more with two more files.

main.lua


system.activate( "multitouch" ) --Activate the multi-touch
display.setStatusBar( display.HiddenStatusBar )

local storyboard = require "storyboard"
storyboard.gotoScene("game"


And finally since I'm using storyboard and telling it to go to game.lua, I need to create that one too.

game.lua

local storyboard = require "storyboard"
local json = require ("json")
local physics = require("physics")
physics.start()
--physics.setDrawMode("hybrid")
local scene = storyboard.newScene()
storyboard.purgeOnSceneChange = true

--This function prevent or alters the phones back button function

local function onKeyEvent( event )
    local keyName = event.keyName
    local phase = event.phase

    if (keyName == "back" and phase == "up") then

        --Put in something else for back button
        return true
    end
    return false
end

--constants

DISPLAY_W = display.contentWidth
DISPLAY_H = display.contentHeight
print("Display="..DISPLAY_W..":"..DISPLAY_H)
DISPLAY_W_CENTER = DISPLAY_W/2
DISPLAY_H_CENTER = DISPLAY_H/2
--Sprites
-- local playerSheetData = { width=163, height=151, numFrames=9, sheetContentWidth=489, sheetContentHeight=453 }
-- local playerSheet = graphics.newImageSheet( "sprites/player.png", playerSheetData )
-- local playerSequenceData =
-- {
    -- {name = "stand",     start = 2,    count = 1},
    -- {name = "walk",     start = 1,    count = 4, time = 500},
    -- {name = "fight",     start = 5,     count = 3, time = 200},
    -- {name = "die",         start = 8,     count = 2, time = 800, loopCount=1}
-- }

--This function runs when the user touches the screen

function performAction(event)
    print(event.phase)
    if event.phase == "began" then
    end
    return true
end


-------------------------------------------

----------- ENTER SCENE EVENT  ------------
-------------------------------------------
function scene:enterScene( event )
    ----------------------------------------------------------------
    -- MAIN LOOP
    ----------------------------------------------------------------
    local function main( event )
       
    end
    --Event Listeners
    Runtime:addEventListener( "touch", performAction ) --User Touches The Screen
    Runtime:addEventListener( "enterFrame", main) -- Enter Loop
    Runtime:addEventListener("key", onKeyEvent) -- User Presses a phone key
    --Runs when you destroy the scene, great for cleanup
    function scene:destroyScene(e)
        Runtime._functionListeners = nil
    end
    --scene:addEventListener("destroyScene",scene)
end

scene:addEventListener("enterScene", scene )

return scene

I place all of these in a folder called Source Code. Inside Source Code I also place a folder called sounds and another called sprites. Then whenever I want to start a new app, I just copy and paste the template folder and bang, I'm building straight away.

I hope this helps you get through those procrastination moments in the same way it helps me.

You can download the template here.

No comments:

Post a Comment