Game Over

Hello guys,

I want to create a Game Over function in my game. That must happen when the ball “touch” the floorr. A sort of collision. Can you please help me with this?

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 49 )

–physics.setDrawMode(“hybrid”)

local background = display.newImage ( “road.jpg” )

local ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = display.contentCenterY+240

local pauseBtn = display.newImageRect ( “pausebtn.gif”, 50, 50 )
pauseBtn.x = 20
pauseBtn.y = 20

local resumeBtn = display.newImageRect ( “resumebtn.gif”, 50, 50 )
resumeBtn.x = 20
resumeBtn.y = 20
resumeBtn.isVisible = false

local leftWall = display.newRect( -10, 0, 10, display.contentHeight )
local rightWall = display.newRect( display.contentWidth, 0, 10, display.contentHeight )
local ceiling = display.newRect( 0, -10, display.contentWidth, 10 )
local floorr = display.newRect( 0, 480, display.contentWidth, 10 )

physics.addBody( leftWall, “static”, { bounce = 0.2 } )
physics.addBody( rightWall, “static”, { bounce = 0.2 } )
physics.addBody( ceiling, “static”, { bounce = 0.2 } )

physics.addBody( ball, { bounce = 0, radius = 40, friction = 1.0 } )

physics.addBody( floorr, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

local lock = physics.newJoint( “weld”, leftWall, floorr, floorr.x, floorr.y ) – keep the floor in place

function moveBall( event )
if event.phase == “began” then
local x, y = ball.x-event.x, ball.y-event.y
ball:applyLinearImpulse( x / 30, -2, ball.x, ball.y )
return true
end
end

function pauseGame( event )
–if end of touch event
if(event.phase == “ended”) then
–pause the physics
physics.pause()
–pause the ball movement
ball:removeEventListener( “touch”, moveBall )
–make pause button invisible
pauseBtn.isVisible = false
–make resume button visible
resumeBtn.isVisible = true
– indicates successful touch
return true
end
end

function resumeGame( event )
–if end of touch event
if(event.phase == “ended”) then
–resume physics
physics.start()
– resume the ball movement
ball:addEventListener( “touch”, moveBall )
–make pause button visible
pauseBtn.isVisible = true
–make resume button invisible
resumeBtn.isVisible = false
– indicates successful touch
return true
end
end

ball.isFixedRotation = true

ball:addEventListener( “touch”, moveBall )

pauseBtn:addEventListener( “touch”, pauseGame )

resumeBtn:addEventListener( “touch”, resumeGame )[/code] [import]uid: 203192 topic_id: 34173 reply_id: 334173[/import]

Hello,
Collisions are easy in Corona; please refer to this guide:

http://developer.coronalabs.com/content/game-edition-collision-detection

There are a few things that can be tricky about it, but it’s not bad. If you have any problems, please follow up to this post.

Best regards,
Brent Sorrentino [import]uid: 200026 topic_id: 34173 reply_id: 135897[/import]

I don’t exactly now which collision is the best, what would you recommend? [import]uid: 203192 topic_id: 34173 reply_id: 135928[/import]

Personally, I prefer the “local” collision listener. Look under “Local collision listeners” on the link I provided. The example should help you figure it out, but if you need specific help, let me know.

Brent [import]uid: 200026 topic_id: 34173 reply_id: 135953[/import]

Now I’m getting this error:

Runtime error
d:\coronasdk\apps\skysoccer\spstart.lua:118: attempt to index local ‘ball’

Do you know how to solve this?

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local background, ball, pauseBtn, resumeBtn, leftWall, rightWall, ceiling, floorr, lock

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 49 )

–physics.setDrawMode(“hybrid”)

function scene:createScene( event )
local screenGroup = self.view

background = display.newImage ( “road.jpg” )
screenGroup:insert( background )

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = display.contentCenterY+240
–ball.myName = “first ball”
screenGroup:insert( ball )

pauseBtn = display.newImageRect ( “pausebtn.gif”, 50, 50 )
pauseBtn.x = 20
pauseBtn.y = 20
screenGroup:insert( pauseBtn )

resumeBtn = display.newImageRect ( “resumebtn.gif”, 50, 50 )
resumeBtn.x = 20
resumeBtn.y = 20
resumeBtn.isVisible = false
screenGroup:insert( resumeBtn )

leftWall = display.newRect( -10, 0, 10, display.contentHeight )
screenGroup:insert( leftWall )
rightWall = display.newRect( display.contentWidth, 0, 10, display.contentHeight )
screenGroup:insert( rightWall )
ceiling = display.newRect( 0, -10, display.contentWidth, 10 )
screenGroup:insert( ceiling )
floorr = display.newRect( 0, 480, display.contentWidth, 10 )
–floorr.myName = “second ball”
screenGroup:insert( floorr )

physics.addBody( leftWall, “static”, { bounce = 0.3 } )
physics.addBody( rightWall, “static”, { bounce = 0.3 } )
physics.addBody( ceiling, “static”, { bounce = 0.3 } )

physics.addBody( ball, { bounce = 0, radius = 40, friction = 1.0 } )

physics.addBody( floorr, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

lock = physics.newJoint( “weld”, leftWall, floorr, floorr.x, floorr.y )
screenGroup:insert( lock )

end

function moveBall( event )
if event.phase == “began” then
local x, y = ball.x-event.x, ball.y-event.y
ball:applyLinearImpulse( x / 30, -2, ball.x, ball.y )
return true
end
end

function pauseGame( event )
–if end of touch event
if(event.phase == “ended”) then
–pause the physics
physics.pause()
–pause the ball movement
ball:removeEventListener( “touch”, moveBall )
–make pause button invisible
pauseBtn.isVisible = false
–make resume button visible
resumeBtn.isVisible = true
– indicates successful touch
return true
end
end

function resumeGame( event )
–if end of touch event
if(event.phase == “ended”) then
–resume physics
physics.start()
– resume the ball movement
ball:addEventListener( “touch”, moveBall )
–make pause button visible
pauseBtn.isVisible = true
–make resume button invisible
resumeBtn.isVisible = false
– indicates successful touch
return true
end
end

–[[local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )

elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end]]–

–[[ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )

floorr.collision = onLocalCollision
floorr:addEventListener( “collision”, floorr )]]–

ball.isFixedRotation = true

ball:addEventListener( “touch”, moveBall )

pauseBtn:addEventListener( “touch”, pauseGame )

resumeBtn:addEventListener( “touch”, resumeGame )

scene:addEventListener( “createScene”, scene )

return scene[/code] [import]uid: 203192 topic_id: 34173 reply_id: 136204[/import]

Hello,
Collisions are easy in Corona; please refer to this guide:

http://developer.coronalabs.com/content/game-edition-collision-detection

There are a few things that can be tricky about it, but it’s not bad. If you have any problems, please follow up to this post.

Best regards,
Brent Sorrentino [import]uid: 200026 topic_id: 34173 reply_id: 135897[/import]

I don’t exactly now which collision is the best, what would you recommend? [import]uid: 203192 topic_id: 34173 reply_id: 135928[/import]

Personally, I prefer the “local” collision listener. Look under “Local collision listeners” on the link I provided. The example should help you figure it out, but if you need specific help, let me know.

Brent [import]uid: 200026 topic_id: 34173 reply_id: 135953[/import]

Now I’m getting this error:

Runtime error
d:\coronasdk\apps\skysoccer\spstart.lua:118: attempt to index local ‘ball’

Do you know how to solve this?

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local background, ball, pauseBtn, resumeBtn, leftWall, rightWall, ceiling, floorr, lock

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 49 )

–physics.setDrawMode(“hybrid”)

function scene:createScene( event )
local screenGroup = self.view

background = display.newImage ( “road.jpg” )
screenGroup:insert( background )

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = display.contentCenterY+240
–ball.myName = “first ball”
screenGroup:insert( ball )

pauseBtn = display.newImageRect ( “pausebtn.gif”, 50, 50 )
pauseBtn.x = 20
pauseBtn.y = 20
screenGroup:insert( pauseBtn )

resumeBtn = display.newImageRect ( “resumebtn.gif”, 50, 50 )
resumeBtn.x = 20
resumeBtn.y = 20
resumeBtn.isVisible = false
screenGroup:insert( resumeBtn )

leftWall = display.newRect( -10, 0, 10, display.contentHeight )
screenGroup:insert( leftWall )
rightWall = display.newRect( display.contentWidth, 0, 10, display.contentHeight )
screenGroup:insert( rightWall )
ceiling = display.newRect( 0, -10, display.contentWidth, 10 )
screenGroup:insert( ceiling )
floorr = display.newRect( 0, 480, display.contentWidth, 10 )
–floorr.myName = “second ball”
screenGroup:insert( floorr )

physics.addBody( leftWall, “static”, { bounce = 0.3 } )
physics.addBody( rightWall, “static”, { bounce = 0.3 } )
physics.addBody( ceiling, “static”, { bounce = 0.3 } )

physics.addBody( ball, { bounce = 0, radius = 40, friction = 1.0 } )

physics.addBody( floorr, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

lock = physics.newJoint( “weld”, leftWall, floorr, floorr.x, floorr.y )
screenGroup:insert( lock )

end

function moveBall( event )
if event.phase == “began” then
local x, y = ball.x-event.x, ball.y-event.y
ball:applyLinearImpulse( x / 30, -2, ball.x, ball.y )
return true
end
end

function pauseGame( event )
–if end of touch event
if(event.phase == “ended”) then
–pause the physics
physics.pause()
–pause the ball movement
ball:removeEventListener( “touch”, moveBall )
–make pause button invisible
pauseBtn.isVisible = false
–make resume button visible
resumeBtn.isVisible = true
– indicates successful touch
return true
end
end

function resumeGame( event )
–if end of touch event
if(event.phase == “ended”) then
–resume physics
physics.start()
– resume the ball movement
ball:addEventListener( “touch”, moveBall )
–make pause button visible
pauseBtn.isVisible = true
–make resume button invisible
resumeBtn.isVisible = false
– indicates successful touch
return true
end
end

–[[local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )

elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end]]–

–[[ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )

floorr.collision = onLocalCollision
floorr:addEventListener( “collision”, floorr )]]–

ball.isFixedRotation = true

ball:addEventListener( “touch”, moveBall )

pauseBtn:addEventListener( “touch”, pauseGame )

resumeBtn:addEventListener( “touch”, resumeGame )

scene:addEventListener( “createScene”, scene )

return scene[/code] [import]uid: 203192 topic_id: 34173 reply_id: 136204[/import]