Corona sdk

19
Variable j = 10 -- global variable local i = 1 -- local variable if i > 20 then local x -- local to the "then" body x = 20 print(x + 2) else print(x) --> 10 (the global one) end print(x) --> 10 (the global one) Types and Values print(type("Hello world")) --> string print(type(10)) --> number print(type(print)) --> function print(type(type)) --> function print(type(true)) --> boolean print(type(nil)) --> nil print(type(type(X))) --> string Control Structures - if then else - while Loop - for Loop

description

พื่นฐานภาษา Lua และ Corona SDK

Transcript of Corona sdk

Page 1: Corona sdk

Variable

j = 10 -- global variable local i = 1 -- local variable if i > 20 then local x -- local to the "then" body x = 20 print(x + 2) else print(x) --> 10 (the global one) end print(x) --> 10 (the global one)

Types and Values print(type("Hello world")) --> string print(type(10)) --> number print(type(print)) --> function print(type(type)) --> function print(type(true)) --> boolean print(type(nil)) --> nil print(type(type(X))) --> string

Control Structures- if then else

! - while Loop

- for Loop

Page 2: Corona sdk

display.newText()Creates a text object with its top-left corner at (left, top). The local origin is at the center of the text; the reference point is initialized to this local origin. By default, the text color is white (255, 255, 255).

Syntax:

local myText = display.newText("Hello World!", 0, 0, native.systemFont, 16)myText:setTextColor(255, 255, 255) myText.text = "Hello!"myText.size = 32

myText.text.x = display.contentCenterXmyText.text.y = display.contentCenterY

display.newText(string, left, top,                                [width, height,] font, size )

Page 3: Corona sdk

display.newImage( )Syntax:

Example:

local myImage = display.newImage( "images/rocket2.png") myImage.x = 150 myImage.y = 250

object = display.newImage(filename [,baseDirectory] [,left,top])

Page 4: Corona sdk

display.newRect()Syntax: display.newRect( left, top, width, height )

Example: local myRectangle = display.newRect(0, 0, 150, 50) myRectangle.strokeWidth = 3 myRectangle:setFillColor(140, 140, 140) myRectangle:setStrokeColor(180, 180, 180)

Page 5: Corona sdk

display.newCircle()Syntax:

Example: local myCircle = display.newCircle( 100, 100, 30 ) myCircle:setFillColor(128,128,128) myCircle.strokeWidth = 5 myCircle:setStrokeColor(128,0,0)  -- red

display.newCircle( xCenter, yCenter, radius )

Page 6: Corona sdk

display.newGroup()Description:Creates a group in which you can add and remove child display objects. Initially, there are no children in a group. The local origin is at the parent’s origin; the reference point is initialized to this local origin.

Syntax: display.newGroup()

Example:local group1 = display.newGroup()local group2 = display.newGroup()

local imgGroup1 = display.newImage("images/group1.png")group1:insert(imgGroup1);group1.x = 10group1.y = 20--print(group1.width)

local imgGroup2 = display.newImage("images/group2.png")

group2:insert(imgGroup2)group2.x = 160group2.y = 20

local rect = display.newRect(0,0, 120, 50)rect.x = group1.width/2rect.y = group1.height/2group1:insert(rect)

local circle = display.newCircle(0,0, 25)circle.x = group2.width/2circle.y = group2.height/2group2:insert(circle)

Page 7: Corona sdk

display.newGroup()

Page 8: Corona sdk

Basic Touch Handlingobject:addEventListener

Syntax:

! object:addEventListener( eventName, listener )

Example: local circle = display.newCircle(0,0, 25) circle.x = display.contentCenterX circle.y = display.contentCenterY

local function myListener( self, event ) if event.phase == "began" then

print( "Listener called with event of type " .. event.name )

elseif event.phase == "moved" then

-- User has moved their finger, while touching

elseif event.phase == "ended" or event.phase == "cancelled" then

-- Touch has ended; user has lifted their finger end

return true -- IMPORTANT end

circle:addEventListener( "touch", myListener )

Page 9: Corona sdk

timer.performWithDelay()timer.performWithDelay()timer.pausetimer.cancel

Description: Call a specified function after a delay.

Syntax: timer.performWithDelay( delay, listener [, iterations] )

Example: local time_txt = display.newText("Time", 0,0, native.systemFont, 25) time_txt.x = display.contentCenterX time_txt.y = display.contentCenterY

local sec = 0 local function listener( event ) sec = sec + 1; time_txt.text = sec end timer.performWithDelay(1000, listener,0 )

Page 10: Corona sdk

timer.pause()Description:

Pauses a timer started with timer.performWithDelay.

Syntax:

! result = timer.pause( timerId )

Example:

local time_txt = display.newText("Time",0,0,native.systemFont,25)time_txt.x = display.contentCenterXtime_txt.y = display.contentCenterY

local time1local sec = 0 local function listener( event ) " sec = sec + 1; time_txt.text = sec if sec >= 10 then "time_txt.text = "pause" "result = timer.pause( time1 ) " end end time1 = timer.performWithDelay(1000, listener, 0 )

Page 11: Corona sdk

timer.resume()Description:

Resumes a timer that was paused with timer.pause.

Syntax:

! result = timer.resume( timerId )

Example:

local function listener( event ) " sec = sec + 1; time_txt.text = sec if sec > 10 then "time_txt.text = "pause" "result = timer.pause(time1) -- Pause "sec = 0" time_txt.text = “resume” "result = timer.resume(time1) end end time1 = timer.performWithDelay(1000, listener, 0 )

Page 12: Corona sdk

audio

Description:

Plays audio.

Plays the audio specified by the audio handle on a channel. If a channel is not explicitly specified, an available channel will be automatically chosen for you if available.

Syntax:

! audio.play( audioHandle [, { [channel=c] [, loops=l] [,

! ! ! duration=d] [, fadein=f] [, onComplete=o]  } ])

Example:

-- audio

local buttonGroup = display.newGroup()

local play = display.newImage("images/play.png")play.x = buttonGroup.widthlocal pause = display.newImage("images/pause.png")pause.x = play.width + 10local resume = display.newImage("images/resume.png") resume.x = (pause.x + resume.width) + 10

buttonGroup:insert(play)buttonGroup:insert(pause)buttonGroup:insert(resume)

buttonGroup.x = display.contentCenterX - 60buttonGroup.y = 400

-- load soundlocal laserSound = audio.loadSound("sounds/MLJ.mp3")local laserChanel

Page 13: Corona sdk

-- play soundlocal function playSound(event)" laserChanel = audio.play(laserSound,{ channel=1, loops=0, fadein=5000}) -- play the laser on any available channelendplay:addEventListener("tap", playSound)

-- pause soundlocal function pauseSound(event)" audio.pause(laserChanel)"endpause:addEventListener("tap", pauseSound)

--resume soundlocal function resumeSound(event)" audio.resume(laserChanel)endresume:addEventListener("tap", resumeSound)

Page 14: Corona sdk

PhysicsOverviewCorona makes it very easy to add physics to your games, even if you've never worked with a physics engine before. While the underlying engine is built around the well-known Box2D, we've taken a radically different design approach that eliminates most of the coding that is traditionally required.

Why use Physics?Say you want your game to have a bouncing ball, Corona's Physics engine makes this easy. In just a few lines of code you can have a ball bouncing around the screen!

You can also use Physics for action collisions. Say you want to have your player walk into a certain area on the screen and have an action take place, you can easily do this by using Corona's Physics engine and using a sensor Physics body.

local physics = require "physics"

physics.setDrawMode()Description:

Selects one of three possible rendering modes for the physics engine. This mode may be changed at any time -- see the "DebugDraw" sample project for an example of how to toggle it on the fly.

Syntax:

physics.setDrawMode( mode )

Example:

physics.setDrawMode( "debug" ) -- shows collision engine outlines onlyphysics.setDrawMode( "hybrid" ) -- overlays collision outlines on normal Corona objectsphysics.setDrawMode( "normal" ) -- the default Corona renderer, with no collision outlines

Page 15: Corona sdk

physics.start()Description:

This function start the physics simulation and should be called before any other physics functions.

Syntax:

! physics.start( noSleep )

Example:

local physics = require "physics"physics.start()

-- step 1 setDrawMode

--physics.setDrawMode( "debug" ) -- shows collision engine outlines only--physics.setDrawMode( "hybrid" ) -- overlays collision outlines on normal Corona objects--physics.setDrawMode( "normal" ) -- the default Corona renderer, with no collision outlines

-- step 2 physics.addBody

local ball = display.newCircle(0,0, 25)ball:setFillColor(252,151,241)ball.x = display.contentCenterXball.y = display.contentCenterY/4physics.addBody( ball, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 25 } )

local left_wall = display.newRect(0,0,4,display.contentHeight)left_wall:setFillColor(167,96,20)physics.addBody( left_wall, "static", { density = 1.0, friction = 0.3, bounce = 0.5} )

local right_wall = display.newRect(0,0,4,display.contentHeight)right_wall:setFillColor(167,96,20)right_wall.x = display.contentWidthphysics.addBody( right_wall, "static", { density = 1.0, friction = 0.3, bounce = 0.5} )

Page 16: Corona sdk

local foot = display.newRect(0,0,display.contentWidth, 4)foot:setFillColor(167,96,20)foot.y = display.contentHeight--foot.rotation = -2physics.addBody( foot, "static", { density = 1.0, friction = 0.3, bounce = 0.5} )

Page 17: Corona sdk

StoryboardScene TemplateDescription:

This scene template can be used as a bare-bones scene to be used with the Storyboard API.

Syntax:Example: SceneTemplate.lua

storyboard.gotoScene()Description:

Used to transition to a specific scene (determined by scene's module name). Internally, this will handle all loading/unloading of the scenes. Scenes may also be explicitly unloaded/removed by the user.

Syntax:

Format 1:

" storyboard.gotoScene( sceneName [, effect, effectTime] )

Example:

Format 1:

local storyboard = require "storyboard"storyboard.gotoScene( "scene1", "fade", 500 )

Page 18: Corona sdk

Build and deploy to device

Page 19: Corona sdk