This is the second installment in our Corona SDK Apple Catcher tutorial. In today's tutorial, we'll add to our interface by creating the game interaction. Read on!
Also available in this series:
- Build an Apple Catcher Game - Interface Creation
- Build an Apple Catcher Game - Adding Interaction
Where We Left Off. . .
Please be sure to check part 1 of the series to fully understand and prepare for this tutorial.
Step 1: Start Button Listeners
This function adds the necesary listeners to the TitleView buttons.
function startButtonListeners(action)
if(action == 'add') then
titleBg:addEventListener('tap', showGameView)
creditsBtn:addEventListener('tap', showCredits)
else
titleBg:removeEventListener('tap', showGameView)
creditsBtn:removeEventListener('tap', showCredits)
end
end
Step 2: Show Credits
The credits screen is shown when the user taps the about button. A tap listener is added to the credits view to remove it.
function showCredits:tap(e)
creditsBtn.isVisible = false
creditsView = display.newImage('credits.png', -130, display.contentHeight-140)
transition.to(creditsView, {time = 300, x = 65, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
end
Step 3: Hide Credits
When the credits screen is tapped, it'll be tweened out of the stage and removed.
function hideCredits:tap(e)
creditsBtn.isVisible = true
transition.to(creditsView, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})
end
Step 4: Show Game View
When the Start button is tapped, the title view is tweened and removed, revealing the game view. There are many parts involved in this view, so we'll split them in the next steps.
function showGameView:tap(e)
transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end})
Step 5: Add Basket
This code places the basket on the stage.
-- Basket
basket = display.newImage('basket.png', 203, 240)
Step 6: Info Bar
Next we add the Info Bar image and create the corresponding TextFields that will display the current score and time remaining.
-- Info Bar
infoBar = display.newImage('infoBar.png', 280)
score = display.newText('0', 65, -2, native.systemFontBold, 14)
score:setTextColor(0)
timeLeft = display.newText('20', 175, -2, native.systemFontBold, 14)
timeLeft:setTextColor(0)
Step 7: Add Physics
Now we add physics to the basket and call the function that will add the game listeners.
-- Add Physics
physics.addBody(basket, 'static')
-- Game Listeners
gameListeners('add')
Step 8: Game Listeners
This function adds the necessary listeners to start the game logic.
function gameListeners(action)
if(action == 'add') then
timerSrc = timer.performWithDelay(500, update, 0)
basket:addEventListener('collision', onCollision)
basket:addEventListener('touch', dragBasket)
else
timer.cancel(timerSrc)
timerSrc = nil
basket:removeEventListener('collision', onCollision)
basket:removeEventListener('touch', dragBasket)
physics.stop()
end
end
Step 9: Drag Basket
The following code uses the phase event property to drag the basket on the x-axis using the player's finger.
-- Drag Basket function dragBasket(e) if(e.phase == 'began') then lastX = e.x - basket.x elseif(e.phase == 'moved') then basket.x = e.x - lastX end end
Step 10: Add Apple or Stick
The update function is executed every half second. Each time a random number is generated and then tested to see if an apple or a stick will be added to the stage. The item is then added to the physics engine and pulled down by gravity automatically.
function update(e)
-- Add Apple or Stick
local rx = math.floor(math.random() * display.contentWidth)
local r = math.floor(math.random() * 4) -- 0, 1, 2, or 3 (3 is stick)
if(r == 3) then
local stick = display.newImage('stick.png', rx, -20)
stick.name = 'stick'
physics.addBody(stick)
else
local apple = display.newImage('apple.png', rx, -40)
apple.name = 'apple'
physics.addBody(apple)
end
Step 11: Decrease Timer
Here we increase the timer variable by one every half second so when it is equal to 2, a second is decreased from the timeLeft textfield.
-- Decrease Timer times = times + 1 if(times == 2) then timeLeft.text = tostring(tonumber(timeLeft.text) - 1) times = 0 end
Step 12: Check for Timer Completion
This part checks if the time available is over and calls an alert if so.
-- Check if timer is over if(timeLeft.text == '0') then alert() end end
Step 13: Apple Collision
When an apple hits the basket it is removed from the stage, the score is updated, and an animation is showed displaying the amount of score raised.
function onCollision(e)
if(e.other.name == 'apple') then
-- Remove Apple
display.remove(e.other)
-- Display animation
local scoreAnim = display.newText('+10', basket.x, basket.y-10, native.systemFontBold, 16)
transition.to(scoreAnim, {time = 600, y = scoreAnim.y - 30, alpha = 0, onComplete = function() display.remove(scoreAnim) scoreAnim = nil end})
-- Update Score
score.text = tostring(tonumber(score.text) + 10)
Step 14: Stick Collision
This uses the same method as the last step, this time decreasing the score.
elseif(e.other.name == 'stick') then
--Remove Stick
display.remove(e.other)
-- Display animation
local scoreAnim = display.newText('-10', basket.x, basket.y-10, native.systemFontBold, 16)
transition.to(scoreAnim, {time = 600, y = scoreAnim.y - 30, alpha = 0, onComplete = function() display.remove(scoreAnim) scoreAnim = nil end})
-- Update Score
score.text = tostring(tonumber(score.text) - 10)
end
end
Step 15: Alert
This function will stop the game and display the final score using the alert background and tweening it.
function alert()
gameListeners('remove')
local alertView = display.newImage('alert.png', 110, 74)
transition.from(alertView, {time = 300, xScale = 0.5, yScale = 0.5})
local totalScore = display.newText(score.text, display.contentCenterX-11, display.contentCenterY + 24, native.systemFontBold, 21)
totalScore:setTextColor(72, 34, 0)
end
Step 16: Call Main Function
In order to start the game, the Main function needs to be called. With the above code in place, we'll do that here:
Main()
Step 17: Loading Screen

The Default.png file is an image that will be displayed right when you start the application while the iOS loads the basic data to show the Main Screen. Add this image to your project source folder, it will be automatically added by the Corona compliler.
Step 18: Icon

Using the graphics you created before you can now create a nice and good looking icon. The icon size for the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.
It doesn't need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.
Step 19: Testing in Simulator

It's time to do the final test. Open the Corona Simulator, browse to your project folder, and then click open. If everything works as expected, you are ready for the final step!
Step 20: Build

In the Corona Simulator, go to File > Build and select your target device. Fill the required data and click build. Wait a few seconds and your app will be ready for device testing and/or submission for distribution!
Conclusion
Experiment with the final result and try to make your own custom version of the game!
I hope you liked this tutorial series and found it helpful. Thank you for reading!
Comments