Corona Coin Drop with Corona SDK Part 3

Hello and welcome to the final part of Corona Coin Drop! In part 3 of this game tutorial series with Corona SDK, we’ll add graphics inside of the scene:create() function. If you need the graphics for Corona Coin Drop, you can download them for free under Templates.

We’ll start adding our graphics inside of function scene:create(). The first graphic we will add is our background.

-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect(sceneGroup, "images/background.png", acw, ach)
background.x = cx
background.y = cy
Next, we’ll add in our floor. The floor is there for visuals only and does not interact with the player.

-- Add in a graphic for our floor. This is the visual piece only
local floor = display.newImageRect(sceneGroup, "images/floor.png", 900, 210)
floor.x = cx
floor.y = bottom - 100

With our floor graphic added, we’ll add in the physics floor next. This is what the player will stand on. We’ve made the floor a static body type and this means the floor does not respond to gravity. If we did not set this, the floor would downwards!

-- I want the player to look like they are 'standing' on the floor so I create a rectangle that acts as the floor. The player will stand on this object.
local floorPhysics = display.newRect(sceneGroup, 0, 0, acw, 50)
floorPhysics.x = cx
floorPhysics.y = floor.y + 30
floorPhysics.myName = "floor"
physics.addBody(floorPhysics, "static")
After our floors, we’ll add a left and right wall to stop the player from sliding off the screen. We’ve also added the static body type to these objects.

-- Create a left/right wall boundary. These objects are static and do not respond to gravity.
local leftWall = display.newRect(sceneGroup, 0, 0, 40, ach)
leftWall.x = left - 25
leftWall.y = cy
leftWall.myName = "wall"
physics.addBody(leftWall, "static")

local rightWall = display.newRect(sceneGroup, 0, 0, 40, ach)
rightWall.x = right + 25
rightWall.y = cy
rightWall.myName = "wall"
physics.addBody(rightWall, "static")

To make the game a little more interesting, we’ll add in two basic obstacles for coins to collide with. This is similar to a Plinko style game. These objects are of the static body type so they will not respond to being hit. I also decided to rotate the objects a bit so the coins cannot get stuck on top of an object.

-- Create two obstacles to give our game some flair
local obstacle1 = display.newImageRect(sceneGroup, "images/obstacle1.png", 180, 180)
obstacle1.x = math.random(200,300)
obstacle1.y = 300
obstacle1.myName = "obstacle"
obstacle1.rotation = 10
physics.addBody(obstacle1, "static")

local obstacle2 = display.newImageRect(sceneGroup, "images/obstacle2.png", 100, 100)
obstacle2.x = math.random(500,600)
obstacle2.y = 625
obstacle2.myName = "obstacle"
obstacle2.rotation = -10
physics.addBody(obstacle2, "static")

We need a back button to allow the player to return to the menu and we’ll place one in the top left. It’s the simple things.

-- Create a menu button to allow the player to go back to the menu
local btn_menu = widget.newButton({
 left = 100,
 top = 200,
 width = 120,
 height = 120,
 defaultFile = "images/btn_menu.png",
 overFile = "images/btn_menu_over.png",
 onEvent = onMenuTouch
btn_menu.x = left + 70
btn_menu.y = top + 70

The player will need a way to see their score so we add in a simple text object in the top right. This will be updated every time the player catches a coin or misses a coin. I also decided to use a bold font type so the text object can stick out a bit better on the background.

-- A visual indicator of the player score
playerScore = display.newText(sceneGroup, "Score: 0", 0, 0, native.systemFontBold, 64)
playerScore.anchorX = 1
playerScore.anchorY = 0
playerScore.x = right - 10
playerScore.y = top + 10

Finally, we’ll wrap up the scene by adding the player. The player has a physics body added to it and I’ve set .isFixedRotation to true. This will stop the player from rotating left/right so it will always remain in an upright position.

-- Add the player!
player = display.newImageRect(sceneGroup, "images/player.png", 315, 332)
player.x = cx
player.y = floor.y - 275
player.myName = "player"
player.isFixedRotation = true

Thank you so much for reading my tutorial series! You can download the full template under Templates if you would like to compare your code or do any kind of troubleshooting. I’ll leave it up to you add additional features to this game such as objects to avoid or maybe even a new control scheme. Leave your comments below and thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *