More on Corona

This post is for the coders on the team. I’m going to break down some of the parts of our sample app and explain what they do. If you haven’t checked out the tools section yet, you should. It has some suggestions and links for getting started.

Here’s the stuff we’re almost always going to have at the top of our lua file. I’ve annotated it so you can see what’s what. (‘Annotated’ means ‘added comments’. In Corona, if you use two dashes, whatever comes after will be ignored by the compiler.)

1
2
3
4
5
6
7
8
9
display.setStatusBar( display.HiddenStatusBar ) -- turns off the device's status bar (signal strength, time, etc)
physics = require("physics") -- tells Corona we're going to be using physics in our game
physics.start( true ) -- turns on the physics engine
physics.setGravity(0, 0) -- sets up gravity. Just like graphing, it goes X,Y. X goes side to side, Y is up and down. (0,9.8) is normal earth gravity
middleX = display.contentWidth/2 -- these lines just make it easy to find the middle of the screen. middleX and middleY are variables that equal half of the screen size
middleY = display.contentHeight/2
math.randomseed(os.time()) -- these two lines are needed if you're going to be using random numbers.
math.random() -- Without them, your numbers might not be too random.
--physics.setDrawMode( "hybrid" ) --take away the two dashes at the very beginning of this line to see your physics bodies.

Next, we set up our walls. Lines 1-4 draw the rectangles, lines 6-9 add physics to the walls (otherwise they’d just be pictures; things would pass right through them).

1
2
3
4
5
6
7
8
9
leftWall = display.newRect( 0, 0, 10, 480 ) --"leftWall" is a variable. I could have named it anything, but "leftWall" seemed to make more sense than "Frankenstein"
rightWall = display.newRect( 310, 0, 10, 480 ) -- The four numbers each are properties of the wall.
topWall = display.newRect( 0, 0, 320, 10 ) -- (x of top left corner, y of top left corner, width, height)
bottomWall = display.newRect( 0, 470, 480, 10 )

physics.addBody(leftWall, "static", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false}) --static bodies stay put
physics.addBody(rightWall, "static", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false}) --dynamic bodies react to each other and to gravity
physics.addBody(topWall, "static", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false}) --kinematic bodies react to each other, but not to gravity
physics.addBody(bottomWall, "static", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false})

The next line adds our background image. Besides display.newImage, there is also display.newRect, display.newCircle, and display.newText.

1
background = display.newImage( "bkg_clouds.png" ) -- for games, we use png files more than jpgs

Next come the functions. Remember, functions are like recipes: they have the instructions for what to do, but they won’t run unless you tell them to. Having a recipe for pizza doesn’t magically make a pizza appear on my plate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function changeColor(event) -- this one says 'event' because it's linked to the 'tap' listener. It will only run when that event happens.
if event.phase == "began" then
randomRed = math.random(0,255)
randomBlue = math.random(0,255)
randomGreen = math.random(0,255)
event.target:setFillColor(randomRed,randomBlue,randomGreen, 255)
event.target:applyForce(math.random(-1000,1000),math.random(-1000,1000),0,0)
end
end

function makeBall()
ball = display.newCircle( math.random(100,300), math.random(100,400), 30 )
physics.addBody(ball, "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2, isSensor = false, radius=30})
ball:applyForce(math.random(-1000,1000),math.random(-1000,1000),0,0)
ball:addEventListener("collision", changeColor) -- this is a listener. Listeners are VERY important. Without them, you get art instead of a game.
end

function makeLotsOfBalls(howMany)
for i=1,howMany do -- this counts from 1 to howMany (10, in our case) and does something each time.
makeBall()
end
end

Finally, this line gets things going. It tells the makeLotsOfBalls function to run 10 times. Each time it runs, it starts the makeBall() function. Phew.

1
makeLotsOfBalls(10) -- if you change the number, you are sending a different value to howMany. makeLotsOfBalls(3) would make 3 balls.

If you have any questions, ask in the comments of this post.

No comments

Comments are closed.