Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Error with Physics and Collision
Started by botafogo0382 Dec 09 2019 05:09 PM

3 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

botafogo0382

[GLOBAL: userInfoPane.html]
botafogo0382
  • Observer

  • 7 posts
  • Corona SDK

"physics.stop() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event"

 

This error is following me to complete my game

 

I'll want to: If vidas < 0, open a menu.

local composer = require( "composer" )
local scene = composer.newScene()

-- include Corona's "physics" library
local physics = require "physics"

local function fimDoJogo()
	
	composer.gotoScene("menu", "fade", 100)
	composer.removeScene("level1")
	return true
end

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )
	local background = display.newImageRect( "ceu.jpg", 640 , 856)
	background.x = display.contentCenterX
	background.y = display.contentCenterY

	local vidas = 1
	local coracao = display.newImageRect( "coracao.png", 30, 30 )
	placarvidas = display.newText(vidas, 320/2,50,native.systemFont,36)
	coracao.x = placarvidas.x
	coracao.y = 20
	

	local aviao = display.newImageRect( "aviao.png", 112, 142 )
	aviao.x = display.contentCenterX
	aviao.y = display.contentCenterY-50

	temponuvem1 = math.random(500,750)
	temponuvem2 = math.random(500,750)


	local nuvem1 = display.newImageRect( "nuvem.png", 190, 190 )
	nuvem1.x = -200 --display.contentCenterX
	nuvem1.y = display.contentHeight-25

	function movernuvem1()
		transition.to(nuvem1, {time=temponuvem1, x= 320,y=nuvem1.y, onComplete= resetamovernuvem1 })
	end
	function resetamovernuvem1()
		transition.to(nuvem1, {time=temponuvem2, x=0, y=nuvem1.y, onComplete = movernuvem1})
	end



	local nuvem2 = display.newImageRect( "nuvem.png", 220, 200 )
	nuvem2.x = -200 --display.contentCenterX
	nuvem2.y = display.contentHeight-450

	function movernuvem2()
		transition.to(nuvem2, {time=750, x= 320,y=nuvem2.y, onComplete= resetamovernuvem2 })
	end
	function resetamovernuvem2()
		transition.to(nuvem2, {time=750, x=0, y=nuvem2.y, onComplete = movernuvem2})
	end

	local physics = require( "physics" )
	physics.start()
	
	physics.addBody( nuvem1, "static" )
	physics.addBody( nuvem2, "static" )

	physics.addBody( aviao, "dynamic", { radius=30, bounce=0.3 } )

	local function pushBalloon()
		aviao:applyLinearImpulse( 0, -0.35, aviao.x, aviao.y )
	end

	movernuvem1()
	movernuvem2()
	aviao:addEventListener( "tap", pushBalloon )

	local function checavida(evento)
		if evento.phase == "ended" then
			vidas = vidas-1
			placarvidas.text = vidas 
			if vidas < 0 then
				physics.removeBody( aviao)
				fimDoJogo()
			end
		end
		
	end
	--aviao.collision = checavida
	Runtime:addEventListener( "collision",checavida )
end


function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		-- Called when the scene is still off screen and is about to move on screen
	elseif phase == "did" then
		-- Called when the scene is now on screen
		-- 
		-- INSERT code here to make the scene come alive
		-- e.g. start timers, begin animation, play audio, etc.
		physics.start()
	end
end

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
		-- Called when the scene is on screen and is about to move off screen
		--
		-- INSERT code here to pause the scene
		-- e.g. stop timers, stop animation, unload sounds, etc.)
		physics.stop()
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

function scene:destroy( event )

	-- Called prior to the removal of scene's "view" (sceneGroup)
	-- 
	-- INSERT code here to cleanup the scene
	-- e.g. remove display objects, remove touch listeners, save state, etc.
	local sceneGroup = self.view
	
	package.loaded[physics] = nil
	physics = nil
end

---------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene


[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 711 posts
  • Corona SDK

Instead of physics.stop( ) ,  trying using

physics.pause( )
  If that doesn't work we'll try the next thing.



[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,103 posts
  • Corona SDK

You can't stop the physics engine during physics events, so pausing the engine as suggested by sporkfin could work. When you want to stop the physics, you could (and should) pause the physics first, then remove any physics bodies and finally stop physics.


  • sporkfin likes this

[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,539 posts
  • Enterprise

Or call your physics.* API's inside a timer if those physics calls are originating from a collision event (either directly in the collision handler, or a function called by the collision handler). 




[topic_controls]
[/topic_controls]