Get $500+ of the best After Effects files, video templates and music for only $20!
Corona SDK: Building a Space Shooter – Final Steps

Corona SDK: Building a Space Shooter – Final Steps

Tutorial Details
  • Technology: Corona SDK
  • Difficulty: Intermediate
  • Completion Time: 30 - 45 Minutes

Welcome to the final tutorial in our Space Shooter game series! In this tutorial, we’ll handle the enter frame logic, collision detection, and the steps to build the final app.


Where We Left Off. . .

Please be sure to check part 1 and part2 of the series to fully understand this tutorial.

Step 1: Update

The update function handles all the actions that occur on enterframe, such as movie clip movements.

function update(e)
    --code
end

Step 2: Move Bullets

The following code identifies when a bullet(s) is on the screen and moves it (them) 10 pixels every frame.

if(bullets.numChildren ~= 0) then
		for i = 1, bullets.numChildren do
			bullets[i].y = bullets[i].y - 10

			-- Destroy Offstage Bullets

			if(bullets[i].y < -bullets[i].height) then
				bullets:remove(bullets[i])
				display.remove(bullets[i])
				bullets[i] = nil
			end
		end
	end

Step 3: Move Enemies

Enemies are also moved using a similar method:

if(enemies.numChildren ~= 0) then
		for i = 1, enemies.numChildren do
			if(enemies[i] ~= nil) then
				enemies[i].y = enemies[i].y + 3

Step 4: Offstage Sprites

When the enemies or bullets are no longer visible on the stage, they are removed to save memory.

if(enemies[i].y > display.contentHeight) then
					enemies:remove(enemies[i])
					display.remove(enemies[i])
					--enemies[i] = nil
				end
			end
		end
	end

Step 5: Show Boss

The boss can be shown at any time in the game. In this example, the boss is shown when the score reaches 50 (destroy one enemy). You can modify this parameter to show the boss later in the game.

if(scoreN == 50 and boss == nil) then
		audio.play(bossSound)
		boss = movieclip.newAnim({'bossA.png','bossA.png','bossA.png','bossA.png','bossA.png', 'bossA.png','bossA.png', 'bossB.png','bossB.png','bossB.png','bossB.png','bossB.png','bossB.png','bossB.png'})
		boss.x = display.contentWidth * 0.5
		boss.name = 'boss'
		physics.addBody(boss)
		boss.bodyType = 'static'
		transition.to(boss, {time = 1500, y = boss.height + (boss.height * 0.5)})
		boss:play()
		boss:addEventListener('collision', collisionHandler)
	end
end

Step 6: Collision Handler

This function handles all collisions, it uses the name parameter declared before to identify which sprites are colliding and performs a different action according to it.

function collisionHandler(e)
	if(e.other.name == 'bullet' and e.target.name == 'enemy') then
		audio.play(explo)
		display.remove(e.other)
		display.remove(e.target)
		scoreN = scoreN + 50
		score.text = 'Score: ' .. tostring(scoreN)
		score:setReferencePoint(display.TopLeftReferencePoint)
		score.x = 1
	elseif(e.other.name == 'bullet' and e.target.name == 'boss') then
		audio.play(explo)
		display.remove(e.other)
		bossHealth = bossHealth - 1
		scoreN = scoreN + 50
		score.text = 'Score: ' .. tostring(scoreN)
		score:setReferencePoint(display.TopLeftReferencePoint)
		score.x = 1
		if(bossHealth <= 0) then
			display.remove(e.target)
			alert('win')
		end
	elseif(e.other.name == 'ship') then
		audio.play(explo)

		display.remove(e.target)

		-- Remove Live

		display.remove(lives[lives.numChildren])

		-- Check for Game Over

		if(lives.numChildren < 1) then
			alert('lose')
		end
	end
end


Step 7: Restart

The next function will reload the game and return to the Title View.

function restart()
	listeners('remove')

	display.remove(bullets)
	display.remove(enemies)
	display.remove(ship)
	display.remove(alertView)

	Main()
end

Step 8: Call Main Function

In order to initially start the game, the Main function needs to be called. With the above code in place, we’ll do that here:

Main()

Step 9: Loading Screen

The Default.png file is an image that will be displayed right when you start the application while iOS loads the basic data to show the Main Screen. Add this image to your project source folder. The Corona compiler will then automatically add it as the load screen.


Step 10: 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 11: 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 12: 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!

Add Comment

Discussion 15 Comments

  1. Scott says:

    Is there no place to download the complete source code?

  2. Dan says:

    Is there a place to download the final source files?

  3. Ben says:

    I am intrigued to see the files as well :)

  4. Staff

    Hey Guys,

    Apologies for the delayed source code upload. I had thought I attached this when I initially published the post, but obviously it wasn’t there. I’ve updated the post and you should be able to download the final code from the button at the top. Sorry for the delay!

    Best,
    Mark

  5. Joakim says:

    Bug in code, if you win and restart the level – nothing happens :)

  6. Martin says:

    Thank you!

  7. shovan says:

    I played this game and tried restarting it on simulator ,it shows “main.lua:222: ‘for’ limit must be a number” on terminal. Could u fix the bug? I am using corona 2011.638 version.

    Thanks.

  8. shovan says:

    How come Author doesn’t fix the bug? Its been a few days already :/. I hope he fixes it soon, otherwise there is no point learning from here (:

  9. Rick says:

    I’d avoid this. Its very, very buggy!

    Not worth looking at the guys tutorials if he’s not going to check they actually work. Same goes for all the other ones he’s posted on here.

    Sorry to sound harsh, but it took me 10 seconds to find a major bug…it probably would have taken Carlos less.

  10. Bionic79 says:

    Very nice tutorial but ,I’m agree ,it’s very buggy.

    Major bug like using local variables inside function mess up the restart function and also other bugs/bad pratice should be fixed if you want this tutorial to be useful for everyone.

    Good work!

  11. RZ says:

    Amesome tutorial Calos … very good example of a simple end to end game for the newbie corona developer … keep up the good work!

    RZ

    London

  12. Nick G says:

    I always check samples like this by dying first and seeing if someone forgot to fix the restart game feature, or retry or whatever.

    It appears, this was forgotten. It was easy to fix for me, but for others who are new this makes the tutotorial kind of crappy as it’s not complete.

    I also see it’s still broken from October 2011, I just came in to retry it see if it was fixed. It’s not.

    Either case, people should still be able to see how basic scoring works, and drag movement, transitions and all that stuff. Just leave the retry crap alone.

    Really, if you are building something like this you’ll need Director 1.4, or the official Storyboard API for corona sdk.

    The retry, you would just change scene to whatever the level is and delay it by 400ms or so. But again, if you are new, you will not know what I just said :)

    ng
    twitter:
    @cellphonegaming

  13. LS says:

    Nice tutorial, thanks a lot !
    But the problem is that restart is not working at all ! does anybody know the exact cause of the problem ?

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.