Developing a Game First Steps with Corona SDK

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.

Download Source Code


8 Comments
  1. You shouldn’t remove objects directly during collision, because you cannot be sure they aren’t part of other ongoing collision. What’s normally used is that you execute the removeSelf call with some delay, so there is enough time to finish all collisions. For example:

    timer.performWithDelay(1, function() event.object2:removeSelf() end, 1)

    • Right you are! You can also use display.remove() to make sure the display object is not nil.

      • Could you tell how the removing with timer works, because by putting it simply like this:

        if(event.object1.myName == “grass” and event.object2.myName == “coin”) then
        timer.performWithDelay(1, function() event.object2:removeSelf() end, 1)
        end

        … as soon as it hits the ground, there comes an error ‘removeSelf’ (a nil value).

        Should there be a listener for those timers, or what?

        Great tutorials!

        • Hi! Instead of using :removeSelf(), I should have used display.remove() because this checks the object first. :removeSelf() just removes it without checking. So, if you replaced it with that, then it should fix the error. For the timers, in most cases you should assign a variable to the timer so you can reference it later, but in this, I decided not to because it’s just a tutorial.

  2. I just watched the game tutorial part 1 which I thought was great so I thought I would come on over to the site and check out the code. Good job all. Keep up the great work.

    I would like to see you all make a simple rpg….

  3. This might be what I’m looking for, but for some reasen there is an error. its not reconizing the event.

Leave a Reply