Hello! We are going to be building on previous posts that have been written on That’s So Panda. So, if you need to brush up on what we’ve covered, head over to Corona SDK Tutorials and look under Game Development to learn more. Let’s get started!
The first step is to create the coins. When we create the coins, we don’t want to just create one or two coins, we want to create a whole lot of coins. We do this by wrapping our coin creation with a timer that doesn’t end. Here’s the code.
function createCoin() coin = display.newCircle( math.random(20,_W-20), -25, math.random(8,14) ) coin:setFillColor(math.random(245,255),math.random(210,223),7) coin:setStrokeColor(0,0,0) physics.addBody( coin, "dynamic" ) coin.myName = "coin" end timer.performWithDelay( 800, createCoin, 0 )
At first glance, this may look a little confusing. In the code, we are calling the createCoin function every 0.8 seconds and a coin is created off screen. When the coin is created, we set the coin to a dynamic physics body so it’s automatically dropped. Corona SDK makes it easy for developers to create games.
Next, we will want to remove the coin when the player ‘grabs’ the coin and if the player misses the coin, we’ll remove the coin when it hits the grass. This functionality will be put inside the function onCollision.
-- If guy collides with coin, remove coin if(event.object1.myName == "guy" and event.object2.myName == "coin") then event.object2:removeSelf(); end -- Remove coin when it reaches the ground if(event.object1.myName == "grass" and event.object2.myName == "coin") then event.object2:removeSelf(); end
Now, we have a coin dropping from the sky that is removed when it touches the player or the grass. In the next post, we’ll actually track how many coins the player grabs and display a simple score.
If you would like to see more tutorials, please leave a comment. This helps keep me motivated to continue to bring high quality tutorials to your site. As always, you can download the full source below.