This is the second installment in our Corona SDK Cut the Rope Inspired tutorial. In today’s tutorial, we’ll add to our interface and code the game interaction. Read on!
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 playBtn:addEventListener('tap', showGameView) creditsBtn:addEventListener('tap', showCredits) else playBtn: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) playBtn.isVisible = false creditsBtn.isVisible = false creditsView = display.newImage('credits.png', 0, display.contentHeight) lastY = titleBg.y transition.to(titleBg, {time = 300, y = (display.contentHeight * 0.5) - (titleBg.height + 50)}) transition.to(creditsView, {time = 300, y = (display.contentHeight * 0.5) + 35, 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) transition.to(creditsView, {time = 300, y = display.contentHeight + 25, onComplete = function() creditsBtn.isVisible = true playBtn.isVisible = true creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end}) transition.to(titleBg, {time = 300, y = lastY}); 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 Instructions
This code places the instructions image in the stage.
-- Instructions ins = display.newImage('instructions.png', 205, 360)
Step 6: Dog
Next we add the Dog image and place it in the center of the stage.
-- Dog dog = display.newImage('dog.png', 128, 408) dog.name = 'dog'
Step 7: Shadow Stars
This part creates the stars in the left corner.
-- Shadow Stars s1 = display.newImage('starShadow.png', 3, 3) s2 = display.newImage('starShadow.png', 33, 3) s3 = display.newImage('starShadow.png', 63, 3)
Step 8: Physics
Now we add physics to the Dog and call the functions that will load the level and add the game listeners.
-- [Set Dog Physics] physics.addBody(dog, 'static', {radius = 14}) dog.isSensor = true loadLevel(1) gameListeners('add')
Step 9: Load Level
The following code uses a double for and a two dimensional table to place the level in the stage. First we check every item in the table to see if it’s a 1 which represents the hanger. Next we chek for 2‘s which represents the stars. In true, a variable is created and physics is added to the body.
function loadLevel(n) for i = 1, 10 do for j = 1, 10 do if(l1[j][i] == 1) then hang = display.newImage('hang.png', i*31, j*32) physics.addBody(hang, 'static', {radius = 8}) bone = display.newImage('bone.png', i*29.5, j*32 + (90)) physics.addBody(bone, 'dynamic', {shape = {-21.5, -5.5, 18.5, -12.5, 20.5, 2.5, -18.5, 11.5}}) elseif(l1[j][i] == 2) then local star = display.newImage('star.png', i*29, j*28) star.name = 'star' physics.addBody(star, 'static', {radius = 15}) star.isSensor = true end end end
Step 10: Add Rope
Another for is used to add the rope. It is composed of 8 parts that are stored in a display Group.
-- Add Rope for k = 1, 8 do local ropePart = display.newImage('rope.png', hang.x - 3, (hang.y-3) + k*10) --10 = rope part height ropePart.name = 'rope' physics.addBody(ropePart) ropeParts:insert(ropePart)
Step 11: Joints
In this part we use physics joints to "glue" the parts of the rope, the rope to the hanger and the rope to the bone.
-- Hang joint if(k == 1) then local hangJoint = physics.newJoint('pivot', hang, ropePart, hang.x - 3, hang.y) end -- Rope Joints if(k > 1) then local joint = physics.newJoint('pivot', ropeParts[k-1], ropeParts[k], hang.x - 3, ropePart.y) end -- Bone joint if(k == 8) then local boneJoint = physics.newJoint('pivot', ropePart, bone, hang.x - 3, ropePart.y) end end end
Step 12: Game Listeners
This function adds the necessary listeners to start the game logic.
function gameListeners(action) if(action == 'add') then bone:addEventListener('collision', starBoneCollision) bg:addEventListener('touch', drawLine) else bone:removeEventListener('collision', starBoneCollision) bg:removeEventListener('touch', drawLine) end end
Step 13: Draw Line
This function uses the touch event and its phases to create a line in the stage when its swiped. It calculates the starting points in the began phase, draws when the finger is moved, and stops when it is lifted.
function drawLine(e) if(e.phase == 'began') then initX = e.x initY = e.y elseif(e.phase == 'moved') then line = display.newLine(initX, initY, e.x, e.y) physics.addBody(line, 'static') line.isSensor = true line:addEventListener('collision', ropeCollision) line.width = 5 lines:insert(line) elseif(e.phase == 'ended') then display.remove(lines) lines = nil lines = display.newGroup() end end
Step 14: Rope Collision
When the line collides with the rope, a part is removed, breaking the joint and making the bone fall.
function ropeCollision(e) if(e.other.name == 'rope') then display.remove(e.other) end end
Step 15: Star-Bone Collision
This function checks if the bone touches a star, fills a shadow star and plays a sound when true.
function starBoneCollision(e) if(e.other.name == 'star') then display.remove(e.other) audio.play(starSnd) collected = collected + 1 if(collected == 1) then local s1 = display.newImage('star.png', -10, -10) elseif(collected == 2) then local s2 = display.newImage('star.png', 21, -10) elseif(collected == 3) then local s3 = display.newImage('star.png', 51, -10) end
Step 16: Bone-Dog Collision
A sound plays and an alert is called when the dog catches the bone.
elseif(e.other.name == 'dog') then display.remove(e.target) audio.play(bite) alert() end end
Step 17: Alert
This function will stop the game and display the Level Complete message by using the alert background and tweening it.
function alert() gameListeners('rmv') alert = display.newImage('alert.png', (display.contentWidth * 0.5) - 105, (display.contentHeight * 0.5) - 55) transition.from(alert, {time = 300, xScale = 0.5, yScale = 0.5}) -- Wait 1 second to stop physics timer.performWithDelay(1000, function() physics.stop() end, 1) end
Step 18: 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 19: 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 20: 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 512×512 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 21: Testing in the 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 22: 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 custom version of the game!
I hope you liked this tutorial series and find it helpful. Thank you for reading!