My game has a character that moves left to right when I tilt but when I go from menu to level 1 to level 2 the tilt based movement stops working. If you need code posted just ask.
Avery [import]uid: 184688 topic_id: 33561 reply_id: 333561[/import]
My game has a character that moves left to right when I tilt but when I go from menu to level 1 to level 2 the tilt based movement stops working. If you need code posted just ask.
Avery [import]uid: 184688 topic_id: 33561 reply_id: 333561[/import]
It would help to see your code.
But taking a blind stab at it, I suspect that your onTilt function is trying access an object that may no longer be available.
[import]uid: 199310 topic_id: 33561 reply_id: 133433[/import]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start()
storyboard.removeScene(“level1”)
storyboard.removeScene(“level2”)
storyboard.removeScene(“level4”)
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group3 = display.newGroup()
local group3 = self.view
physics.setGravity(0,20)
local background = display.newImageRect( “bg.png”, display.contentWidth, display.contentHeight )
group3:insert(background)
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0
–clouds
local cloud = display.newImage(“cloud 2.png”)
group3:insert(cloud)
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”)
group3:insert(cloud)
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”)
group3:insert(cloud)
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”)
group3:insert(mandown)
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.25, friction=.75})
–dangerous stuff
local ss= display.newImage(“ss.png”)
physics.addBody(ss,“static”)
group3:insert(ss)
local transLeft
local function transRight()
transition.to(ss, {time=3000, y=265, onComplete=transLeft})
end
transLeft = function()
transition.to(ss, {time=3000, y=63, onComplete=transRight})
end
transRight()
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”)
group3:insert(spikesl)
local spikesd = display.newImage(“spikes.png”)
group3:insert(spikesd)
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”)
group3:insert(restart)
restart.width=60
restart.height = 60
restart.x = 450
restart.y = 30
function restart:touch( event )
mandown.rotation = 0
physics.start()
mandown.y = 260
mandown.x = 90
physics.setGravity(0,20)
end
restart:addEventListener(“touch”)
local home = display.newImage(“home.png”)
group3:insert(home)
home.width=60
home.height = 60
home.x = 400
home.y = 30
function home:touch( event )
if event.phase == “ended” then
storyboard.purgeScene( level2)
storyboard.gotoScene( “menu”, “fade”, 500 )
end
return true – important.
end
home:addEventListener(“touch”, home)
local up = display.newImage(“up.png”)
group3:insert(up)
local down = display.newImage(“down.png”)
group3:insert(down)
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”)
group3:insert(plat1)
physics.addBody(plat1, “static”,{friction=10})
local plat2 = display.newImage(“plat.png”)
group3:insert(plat2)
physics.addBody(plat2, “static”,{friction=10})
local plat3 = display.newImage(“plat.png”)
group3:insert(plat3)
physics.addBody(plat3, “static”,{friction=10})
–arrows
local rightg = display.newImage(“right.png”)
group3:insert(rightg)
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 )
–walls
local wallleft = display.newRect(-10, -60, 10, 600 )
wallleft:setFillColor( 255 )
group3:insert(wallleft)
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 )
group3:insert(wallright)
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 )
group3:insert(walltop)
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 )
group3:insert(wallbottom)
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 )
–finish Level 3
local finish = display.newImage(“ring.png”)
group3:insert(finish)
finish.x = 300
physics.addBody(finish, “static”, {bounce=0})
function finishcollision (event)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
finish.x = 760
end
, 1)
– 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
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
group3:removeSelf()
storyboard.gotoScene( “level4”, “fade”, 500 )
end
, 1)
elseif 2 == i then
finish.x = 300
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 )
plat1.x = 1000
plat2.x = 340
plat2.y= 290
plat3.x = 103
plat3.y= 285
plat3.width = 80
–ss
ss.x=180
ss.y=200–top
spikesl.x = 190
spikesl.y =20
spikesl.width = 50
spikesl.height = 100
spikesl:rotate( 270)
–bottom
spikesd.x = 200
spikesd.y = 305
spikesd.width = 50
spikesd.height = 100
spikesd:rotate( 90)
rightg.y = 90
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group3 = self.view
physics.start()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group3 = self.view
package.loaded[physics] = nil
physics = nil
end
– “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 [import]uid: 184688 topic_id: 33561 reply_id: 133437[/import]
Remove the event when leaving the scene…
– Called when scene is about to move offscreen:
function scene:exitScene( event )
Runtime:removeEventListener ("accelerometer", onTilt)
end
[import]uid: 9592 topic_id: 33561 reply_id: 133473[/import]
Thank you so much I wish I thought of that!
Avery [import]uid: 184688 topic_id: 33561 reply_id: 133498[/import]
You are welcome… i had the same issue when i have used the tilt event the first time!
Br,
Alen [import]uid: 9592 topic_id: 33561 reply_id: 133499[/import]
It would help to see your code.
But taking a blind stab at it, I suspect that your onTilt function is trying access an object that may no longer be available.
[import]uid: 199310 topic_id: 33561 reply_id: 133433[/import]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start()
storyboard.removeScene(“level1”)
storyboard.removeScene(“level2”)
storyboard.removeScene(“level4”)
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group3 = display.newGroup()
local group3 = self.view
physics.setGravity(0,20)
local background = display.newImageRect( “bg.png”, display.contentWidth, display.contentHeight )
group3:insert(background)
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0
–clouds
local cloud = display.newImage(“cloud 2.png”)
group3:insert(cloud)
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”)
group3:insert(cloud)
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”)
group3:insert(cloud)
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”)
group3:insert(mandown)
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.25, friction=.75})
–dangerous stuff
local ss= display.newImage(“ss.png”)
physics.addBody(ss,“static”)
group3:insert(ss)
local transLeft
local function transRight()
transition.to(ss, {time=3000, y=265, onComplete=transLeft})
end
transLeft = function()
transition.to(ss, {time=3000, y=63, onComplete=transRight})
end
transRight()
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”)
group3:insert(spikesl)
local spikesd = display.newImage(“spikes.png”)
group3:insert(spikesd)
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”)
group3:insert(restart)
restart.width=60
restart.height = 60
restart.x = 450
restart.y = 30
function restart:touch( event )
mandown.rotation = 0
physics.start()
mandown.y = 260
mandown.x = 90
physics.setGravity(0,20)
end
restart:addEventListener(“touch”)
local home = display.newImage(“home.png”)
group3:insert(home)
home.width=60
home.height = 60
home.x = 400
home.y = 30
function home:touch( event )
if event.phase == “ended” then
storyboard.purgeScene( level2)
storyboard.gotoScene( “menu”, “fade”, 500 )
end
return true – important.
end
home:addEventListener(“touch”, home)
local up = display.newImage(“up.png”)
group3:insert(up)
local down = display.newImage(“down.png”)
group3:insert(down)
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”)
group3:insert(plat1)
physics.addBody(plat1, “static”,{friction=10})
local plat2 = display.newImage(“plat.png”)
group3:insert(plat2)
physics.addBody(plat2, “static”,{friction=10})
local plat3 = display.newImage(“plat.png”)
group3:insert(plat3)
physics.addBody(plat3, “static”,{friction=10})
–arrows
local rightg = display.newImage(“right.png”)
group3:insert(rightg)
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 )
–walls
local wallleft = display.newRect(-10, -60, 10, 600 )
wallleft:setFillColor( 255 )
group3:insert(wallleft)
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 )
group3:insert(wallright)
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 )
group3:insert(walltop)
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 )
group3:insert(wallbottom)
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 )
–finish Level 3
local finish = display.newImage(“ring.png”)
group3:insert(finish)
finish.x = 300
physics.addBody(finish, “static”, {bounce=0})
function finishcollision (event)
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
finish.x = 760
end
, 1)
– 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
timer.performWithDelay( 1, – 1 millisecond delay
function() – anonymous function to perform the update
group3:removeSelf()
storyboard.gotoScene( “level4”, “fade”, 500 )
end
, 1)
elseif 2 == i then
finish.x = 300
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 )
plat1.x = 1000
plat2.x = 340
plat2.y= 290
plat3.x = 103
plat3.y= 285
plat3.width = 80
–ss
ss.x=180
ss.y=200–top
spikesl.x = 190
spikesl.y =20
spikesl.width = 50
spikesl.height = 100
spikesl:rotate( 270)
–bottom
spikesd.x = 200
spikesd.y = 305
spikesd.width = 50
spikesd.height = 100
spikesd:rotate( 90)
rightg.y = 90
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group3 = self.view
physics.start()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group3 = self.view
package.loaded[physics] = nil
physics = nil
end
– “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 [import]uid: 184688 topic_id: 33561 reply_id: 133437[/import]
Remove the event when leaving the scene…
– Called when scene is about to move offscreen:
function scene:exitScene( event )
Runtime:removeEventListener ("accelerometer", onTilt)
end
[import]uid: 9592 topic_id: 33561 reply_id: 133473[/import]
Thank you so much I wish I thought of that!
Avery [import]uid: 184688 topic_id: 33561 reply_id: 133498[/import]
You are welcome… i had the same issue when i have used the tilt event the first time!
Br,
Alen [import]uid: 9592 topic_id: 33561 reply_id: 133499[/import]