Tuesday, May 15, 2012

Corona SDK code dump

Here's a couple of small sample scripts I wrote for a presentation on Corona. They're pretty small and simple, but give a decent overview of how to do some basic stuff with Corona. I meant to write a lot more about them here, but I haven't had the time lately, so the comments will have to suffice. Enjoy!

Also, some pretty useful links at the bottom here =D

Samples

Here's a brief sample of a LUA script that creates basic display objects:
-- basic object types
local rectangleObject = display.newRect       (10, 10,  160, 60)
local roundedObject   = display.newRoundedRect(10, 80,  160, 60, 20)
local circleObject    = display.newCircle     (90, 180, 30)
local lineObject      = display.newLine       (10, 220, 170, 280)
local textObject      = display.newText       ("Test text!", 10, 290, "Arial", 24)

-- setting color!
rectangleObject:setFillColor(200, 100, 100)
roundedObject  :setFillColor(100, 200, 100)
circleObject   :setFillColor(100, 100, 200)

-- a few handy properties
textObject.x = 300
textObject.y = 300
textObject.rotation = 45

-- some handy display properties
circleObject.x = display.contentWidth  / 2
circleObject.y = display.contentHeight / 2

-- a note on . vs : for method calls, . is a static method, where : is for an instance method
-- it could also be said that : as in obj:Move( x, y ) is the same as obj.Move(obj, x, y)

Here's another example showing events with Corona:

-- create a basic object
local obj = display.newCircle(display.contentWidth/2, display.contentHeight/2,  40)
obj:setFillColor(200, 100, 100)

-- define some functions to use for catching events
local function onEnterFrame( aEvent )
    obj.y = display.contentHeight/2 + math.sin(aEvent.time * 0.01) * 20
end

local function onTouch( aEvent )
    print( "Caught a touch!" )
end

-- attach the functions to the events
Runtime:addEventListener( "enterFrame", onEnterFrame )
obj    :addEventListener( "touch",      onTouch      )

An example using transitions and time delayed events:
-- create a basic object
local obj = display.newCircle(display.contentWidth/2, display.contentHeight/2,  40)
obj:setFillColor(200, 100, 100)

-- Create a transition! It's easy!
transition.to(obj, {time=2000, x=0, y=0, alpha=0})

-- also, create a time delayed event
local function onTimer( aEvent )
    transition.to(obj, {time=2000, x=display.contentWidth/2, y=display.contentHeight/2, alpha=1})
end
timer.performWithDelay(3000, onTimer)

And lastly, a quick example using physics:

-- set up physics
physics = require("physics")
physics.start      ( true )
physics.setGravity ( 0, 10 );
physics.setDrawMode( "hybrid" )

-- create some basic objects
local ball = display.newCircle(display.contentWidth/2, display.contentHeight/2,   40)
local wall = display.newRect  (0, display.contentHeight-40, display.contentWidth, 40)
ball:setFillColor(200, 100, 100)
wall:setFillColor(100, 200, 100)

-- attach them to physics
physics.addBody( ball,           { density = 1.0, friction = 0.3, bounce = 0.2, radius = 40 } )
physics.addBody( wall, "static", { density = 1.0, friction = 0.3, bounce = 0.2 } )

-- attach the physics gravity to the accelerometer of the device
local function onTilt( event )
    physics.setGravity( 10 * event.xGravity, -10 * event.yGravity )
end
Runtime:addEventListener( "accelerometer", onTilt )

And a quick interactive demo:
-- set up physics
physics = require("physics")
physics.start      (true)
physics.setGravity (0, 0);
physics.setDrawMode("hybrid")

-- define variables
local left      = -1000
local right     =  1000
local top       = -1000
local bottom    =  1000
local minWidth  =  50
local minHeight =  50
local maxWidth  =  150
local maxHeight =  150
local touchDown = {x = 0, y = 0}

-- create the ball that the user directs
local ball = display.newCircle(0, 0, 40)
ball   :setFillColor(100, 200, 100)
physics.addBody     (ball, {density = 1.0, friction = 0.3, bounce = 0.2, radius = 40})

-- create a decorative background and a physical foreground
for i=1,100 do
    -- create a random decorative block
    local x      = math.random(left,      right    )
    local y      = math.random(top,       bottom   )
    local width  = math.random(minWidth,  maxWidth )
    local height = math.random(minHeight, maxHeight)
    
    local backBlock = display.newRect(x, y, width, height)
    backBlock:setFillColor(100, 100, 100)
    
    -- create a random interactive block
    x      = math.random(left,      right    )
    y      = math.random(top,       bottom   )
    width  = math.random(minWidth,  maxWidth )
    height = math.random(minHeight, maxHeight)
    
    local frontBlock = display.newRect(x, y, width, height)
    frontBlock:setFillColor(200, 200, 200)
    physics   .addBody     (frontBlock, {density = 1.0, friction = 0.3, bounce = 0.2})
end

-- look for user flicks
local function onTouch(aEvent)
    if aEvent.phase == "began" then
        touchDown.x = aEvent.x
        touchDown.y = aEvent.y
    end
    if aEvent.phase == "ended" then
        ball:setLinearVelocity(aEvent.x - touchDown.x, aEvent.y - touchDown.y)
    end
end
Runtime:addEventListener("touch", onTouch)

Links