OK here is the main level where all the action is
[lua]-----------------------------------------------------------------------------------------
– level1.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
– BEGINNING OF YOUR IMPLEMENTATION
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
local background = display.newImageRect( “bg.png”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0
–clouds
local cloud = display.newImage(“cloud 2.png”)
cloud.x=900
local transLeft
local function transRight()
transition.to(cloud, {time=30000, x=-100, onComplete=transLeft})
end
transLeft = function()
transition.to(cloud, {time=30000, x=900, onComplete=transRight})
end
transRight()
local cloud = display.newImage(“cloud 3.png”)
cloud.x=0
local transLeft
local function transRight()
transition.to(cloud, {time=30000, x=800, onComplete=transLeft})
end
transLeft = function()
transition.to(cloud, {time=30000, x=0, onComplete=transRight})
end
transRight()
local cloud = display.newImage(“cloud 1.png”)
cloud.x=600
local transLeft
local function transRight()
transition.to(cloud, {time=30000, x=-100, onComplete=transLeft})
end
transLeft = function()
transition.to(cloud, {time=30000, x=600, onComplete=transRight})
end
transRight()
–character
local mandown = display.newImage(“mandown.png”)
mandown.isFixedRotation = true
mandown.y = 260
mandown.x = 90
function mandown:touch( event )
if event.phase == “began” then
self.markX = self.x – store x location of object
self.markY = self.y – store y location of object
elseif event.phase == “moved” then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y – move object based on calculations above
end
return true
end
– make ‘myObject’ listen for touch events
mandown:addEventListener( “touch”, mandown )
physics.addBody(mandown,“dynamic”, {bounce=0.3, friction=1})
–dangerous stuff
local ss= display.newImage(“ss.png”)
ss.y=700
physics.addBody(ss,“static”)
function sscollision (event)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
ss.collision = sscollision
ss:addEventListener( “collision”, ss )
local spikesl = display.newImage(“spikes.png”)
spikesl.x = 300
spikesl.y =100
spikesl.width = 50
spikesl.height = 100
local spikesd = display.newImage(“spikes.png”)
spikesd.x = 50
spikesd.y = 3
spikesd.width = 50
spikesd.height = 100
spikesd:rotate( 270 )
physics.addBody( spikesd, “static”)
function spikesdcollision (event)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
spikesd.collision = spikesdcollision
spikesd:addEventListener( “collision”, spikesd )
physics.addBody( spikesl, “static”)
function spikeslcollision (event)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
spikesl.collision = spikeslcollision
spikesl:addEventListener( “collision”, spikesl )
– controls
local restart = display.newImage(“restart.png”)
restart.width=60
restart.height = 60
restart.x = 450
restart.y = 30
function restart:touch( event )
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
physics.setGravity(0,20)
end
restart:addEventListener(“touch”)
local home = display.newImage(“home.png”)
home.width=60
home.height = 60
home.x = 400
home.y = 30
function home:touch( event )
if event.phase == “ended” then
storyboard.gotoScene( “menu” )
end
return true – important.
end
home:addEventListener(“touch”, home)
local up = display.newImage(“up.png”)
local down = display.newImage(“down.png”)
up.x = 450
up.y = 280
up.width =75
up.height = 80
down.x = 30
down.y = 280
down.width =75
down.height = 80
function up:touch( event )
mandown.rotation = 180
physics.setGravity(0,-20)
mandown.rotation = 180
end
function down:touch( event )
mandown.rotation = 0
physics.setGravity(0,20)
mandown.rotation = 0
end
up : addEventListener(“touch”)
down : addEventListener(“touch”)
function onTilt(event)
mandown.x = mandown.x+17*-(event.yGravity)
print(event.yGravity)
end
Runtime:addEventListener (“accelerometer”, onTilt)
–platforms
local plat1 = display.newImage(“plat.png”)
plat1.x = 110
plat1.width=100
plat1.y = 290
physics.addBody(plat1, “static”,{friction=10})
local plat2 = display.newImage(“plat.png”)
plat2.x = 170
physics.addBody(plat2, “static”,{friction=10})
local plat3 = display.newImage(“plat.png”)
plat3.x = 290
plat3.y = 290
physics.addBody(plat3, “static”,{friction=10})
–arrows
local rightg = display.newImage(“right.png”)
rightg.x=100
rightg.y = 400
rightg.height = 50
rightg.width = 50
physics.addBody( rightg , “static”, { density=10000.0, friction=10})
function rightgcollision (event)
physics.setGravity(10,-5)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
mandown.rotation =90
end
, 1)
end
rightg.collision = rightgcollision
rightg:addEventListener( “collision”, rightg )
local upg = display.newImage(“upg.png”)
upg.x=700
upg.y = 400
upg.height = 50
upg.width = 50
physics.addBody(upg , “static”, { density=10000.0, friction=10})
function upgcollision (event)
physics.setGravity(0,-15)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
mandown.rotation =180
end
, 1)
end
upg.collision = upgcollision
upg:addEventListener( “collision”, upg )
–finish Level 1
local finish = display.newImage(“ring.png”)
finish.x = 360
physics.addBody(finish, “static”, {bounce=0})
function finishcollision (event)
local function listener( event )
finish.x=700
end
timer.performWithDelay( 1, listener )
– Handler that gets notified when the alert closes
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
finish:removeSelf()
finish = nil
rightg.y = 150
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
–platforms
plat1.x = 100
plat2.x = 60
plat2.y= 100
plat3.width = 120
plat1.width = 75
–top
spikesl.x = 190
spikesl.y =96
spikesl.width = 50
spikesl.height = 100
spikesl:rotate( 270)
local finishtwo= display.newImage(“ring.png”)
finishtwo.x = 330
physics.addBody(finishtwo, “static”, {bounce=0})
–bottom
spikesd.x = 180
spikesd.y = 310
spikesd.width = 50
spikesd.height = 100
spikesd:rotate( 180 )
function finishtwocollision (event)
local function listener( event )
finishtwo.x=700
end
timer.performWithDelay( 1, listener )
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
finishtwo:removeSelf()
finishtwo = nil
local function listener( event )
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
rightg.x = 90
rightg.y=80
local finish3 = display.newImage(“ring.png”)
finish3.x = 300 physics.addBody(finish3, “static”, {bounce=0})
–gravity switchers
rightg.x = 90
rightg.y=80
–platforms
plat1.x = 100
plat2.x = 320
plat2.y= 290
plat3.x=190
plat3.y=1200
plat3.rotation=270
–ss
ss.x=180
ss.y=260
local transLeft
local function transRight()
transition.to(ss, {time=2000, y=60, onComplete=transLeft})
end
transLeft = function()
transition.to(ss, {time=2000, y=260, onComplete=transRight})
end
transRight()
–top
spikesl.x = 190
spikesl.y =20
spikesl.width = 50
spikesl.height = 100
spikesl:rotate( 0)
–bottom
spikesd.x = 190
spikesd.y = 300
spikesd.width = 50
spikesd.height = 100
end
timer.performWithDelay(
1, listener )
elseif 2 == i then
finishtwo.x = 360
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
end
end
– Show alert with two buttons
local alert = native.showAlert( “Gravity Man”, “Congrats!”, { “NEXT”, “Replay” }, onComplete )
end
finishtwo.collision = finishtwocollision
finishtwo:addEventListener( “collision”, finishtwo )
–If you click replay
elseif 2 == i then
finish.x = 360
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
end
end
– Show alert with two buttons
local alert = native.showAlert( “Gravity Man”, “Congrats!”, { “NEXT”, “Replay” }, onComplete )
end
finish.collision = finishcollision
finish:addEventListener( “collision”, finish )
–walls
local wallleft = display.newRect(-10, -60, 10, 600 )
wallleft:setFillColor( 255 )
physics.addBody( wallleft , “static”, { density=10000.0, friction=10})
function wallleftcollision (event)
physics.setGravity(0,20)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
wallleft.collision = wallleftcollision
wallleft:addEventListener( “collision”, wallleft )
local wallright = display.newRect(480, -60, 10, 600 )
wallright:setFillColor( 255 )
physics.addBody( wallright, “static”, { density=10000.0, friction=10} )
function wallrightcollision (event)
physics.setGravity(0,20)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
wallright.collision = wallrightcollision
wallright:addEventListener( “collision”, wallright )
local walltop = display.newRect(0, -10, 600, 10 )
walltop:setFillColor( 255 )
physics.addBody( walltop , “static”, { density=10000.0, friction=10})
function walltopcollision (event)
physics.setGravity(0,20)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
walltop.collision = walltopcollision
walltop:addEventListener( “collision”, walltop )
local wallbottom = display.newRect(0, 320, 600, 10 )
wallbottom:setFillColor( 255 )
physics.addBody( wallbottom, “static”, {friction=10})
function wallbottomcollision (event)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
physics.setGravity(0,20)
mandown.rotation = 0
mandown.y = 260
mandown.x = 90
end
, 1)
end
wallbottom.collision = wallbottomcollision
wallbottom:addEventListener( “collision”, wallbottom )
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
physics.start()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
print( “Hello world!” )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
print( “Hello world!” )
– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
print( “Hello world!” )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene[/lua]
Here is the main menu
[lua]-----------------------------------------------------------------------------------------
– menu.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “widget” library
local widget = require “widget”
– forward declarations and other locals
local playBtn
– ‘onRelease’ event listener for playBtn
local function onPlayBtnRelease()
– go to level1.lua scene
storyboard.gotoScene( “level1”, “fade”, 500 )
return true – indicates successful touch
end
– BEGINNING OF YOUR IMPLEMENTATION
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
– display a background image
local background = display.newImageRect( “bg.png”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0
– create/position logo/title image on upper-half of the screen
local titleLogo = display.newImageRect( “logo.png”, 264, 42 )
titleLogo:setReferencePoint( display.CenterReferencePoint )
titleLogo.x = display.contentWidth * 0.5
titleLogo.y = 20
local instructions = display.newImage( “instructions.jpg”, 264, 42 )
instructions.y = 150
instructions.x = display.contentWidth * 0.5
– create a widget button (which will loads level1.lua on release)
playBtn = widget.newButton{
label=“Play Now”,
labelColor = { default={255}, over={128} },
default=“button.png”,
over=“button-over.png”,
width=154, height=40,
onRelease = onPlayBtnRelease – event listener function
}
playBtn:setReferencePoint( display.CenterReferencePoint )
playBtn.x = display.contentWidth*0.5
playBtn.y = display.contentHeight - 50
– all display objects must be inserted into group
group:insert( background )
group:insert( titleLogo )
group:insert( playBtn )
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
– INSERT code here (e.g. start timers, load audio, start listeners, etc.)
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
if playBtn then
playBtn:removeSelf() – widgets must be manually removed
playBtn = nil
end
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene [import]uid: 184688 topic_id: 33302 reply_id: 132329[/import]