Launching a Rocket with Corona SDK
Hello and welcome to another tutorial for Corona SDK! In this tutorial, we’ll cover how to launch a rocket ship using Corona SDK. We’ll cover the basics by providing a platform, a rocket, and a launch button for our player. The player can then send the rocket upwards by tapping and holding the launch button. When the player lets go of the launch button, the rocket will no longer be propelled upwards and it will fall back down to the platform.
On a more technical level, we’ll be using Corona’s display objects to represent the items in the game. We’ll take advantage of the physics library by giving our platform and rock physic bodies. The rocket ship will be propelled upwards by using the applyForce() function. This tutorial is using a configuration of 320×480 for learning purposes.
To get started, we’ll include our physics library and start it up by calling physics.start().
local physics = require( "physics" ) physics.start()
We’ll also include one variable that will let us know whether or not to launch the rocket. If it’s true, we’ll apply force to the rocket. If it’s false, we won’t.
local doLaunch = false
Next, we’ll create our platform and our rocket. Our platform is a rectangle object set at the bottom of the screen. The platform is set to a static body so it’ll stay in position. The rocket is a circle object that sits on top of the platform. The default dynamic body is more than enough for this tutorial.
local platform = display.newRect(0, 0, 100, 20)
platform.x = 160
platform.y = 440
platform:setFillColor(1)
physics.addBody( platform, "static", {friction=0, bounce = 0} )
local rocket = display.newCircle(0,0,20)
rocket.x = 160
rocket.y = platform.y - 30
physics.addBody( rocket )
After our objects are setup, we can then setup our moveRocket() function. This function will run on every frame and it will apply a negative yForce to our rocket. The applyForce() function accepts the parameters of applyForce(xForce, yForce, bodyX, bodyY). By using a negative yForce in this situation, we are sending our rocket up.
We’ll also use a if/then statement to check if the variable doLaunch is true or false. When doLaunch is true, the code will apply the force to the rocket.
local function moveRocket() if(doLaunch) then rocket:applyForce( 0, -0.185, rocket.x, rocket.y ) end end
To launch our rocket, we’ll create a function called launchTouch and a circle object called launchButton. When launchButton is touched, we’ll set doLaunch to true. When the player removes their finger from the button, doLaunch is set to false.
local function launchTouch(event)
if(event.phase == "began") then
doLaunch = true
elseif(event.phase == "ended" or event.phase == "cancelled") then
doLaunch = false
end
end
local launchButton = display.newCircle(0,0,20)
launchButton.x = 290
launchButton.y = 450
launchButton:setFillColor(0,1,0)
launchButton:addEventListener("touch", launchTouch)
Finally, we have the event listener that will call the function moveRocket. This function will run every frame of our game, which is 30 frames per second.
Runtime:addEventListener("enterFrame", moveRocket)
And that’s it for launching a rocket with Corona SDK! For these types of tutorials, I find it helpful to provide the entire code in one lump. You can copy/paste this code in your project as you see fit. Otherwise, let me know if you have questions or comments below. Thanks for reading!
local physics = require( "physics" )
physics.start()
--physics.setDrawMode( "hybrid" ) -- enable this to see the physics boundaries
local doLaunch = false
local platform = display.newRect(0, 0, 100, 20)
platform.x = 160
platform.y = 440
platform:setFillColor(1)
physics.addBody( platform, "static", {friction=0, bounce = 0} )
local rocket = display.newCircle(0,0,20)
rocket.x = 160
rocket.y = platform.y - 30
physics.addBody( rocket )
local function moveRocket()
if(doLaunch) then
rocket:applyForce( 0, -0.185, rocket.x, rocket.y )
end
end
local function launchTouch(event)
if(event.phase == "began") then
doLaunch = true
elseif(event.phase == "ended" or event.phase == "cancelled") then
doLaunch = false
end
end
local launchButton = display.newCircle(0,0,20)
launchButton.x = 290
launchButton.y = 450
launchButton:setFillColor(0,1,0)
launchButton:addEventListener("touch", launchTouch)
Runtime:addEventListener("enterFrame", moveRocket)
hi, can i change the white circle into real rocket image? the one that is launching up? if it is possible, then how can i do it?thanks in advance, and great code. a big help for my assignment in school.
Hello Meg, you’ll need to swap out the rectangle and circle with display.newImageRect(). You’ll need to specify the location of your image.