Остановить физику для одного объекта в Corona SDK?

У меня проблема при попытке перезапустить игру. Я приостанавливаю физику, затем, когда пользователь нажимает кнопку, физика должна перезапуститься. Когда я помещаю в phys.start(), хотя игра вылетает. Итак, мне было интересно, можно ли приостановить один физический объект, а затем изменить его положение на экране. Я новичок, поэтому любой ответ будет полезным.

local physics = require( "physics")
physics.start( )


local crate1 =  display.newRect( display.contentWidth/2,display.contentHeight/2, 40,40)
physics.addBody( crate1, { density=4.0, friction=0.3, bounce=.4} )
crate1.bodyType = "dynamic"
crate1.isBodyActive = true
crate1:setFillColor( 1,0,.3)


sky = display.newRect( display.contentWidth/2, .5, display.contentWidth, 0.000000000000000000001)
physics.addBody( sky, {density=4.0, friction=0.3, bounce=1.5} )
sky.bodyType = "static"



ground = display.newRect( display.contentWidth/2, display.contentHeight, display.contentWidth, .00000000001 )
 physics.addBody( ground, {density=4.0, friction=0.3, bounce=1.5 } )
 ground.bodyType = "static"


 rightWall = display.newRect( display.contentWidth, display.contentHeight/2, 1, display.contentHeight )
 physics.addBody( rightWall, {density=4.0, friction=0.3, bounce=1.5} )
 rightWall.bodyType = "static"


 leftWall = display.newRect(1, display.contentHeight/2, .0000000001, display.contentHeight )
 physics.addBody( leftWall, {density=4.0, friction=0.3, bounce=1.5} )
 leftWall.bodyType = "static"


physics.setGravity( 0, 3 )


local function handleCollisionOnDelay( event ) 
  local obj = event.source.object

  obj.isBodyActive = false
end


local function onLocalCollision( self, event )
  if ( event.phase == "began" ) then
    local dr = timer.performWithDelay( 10, handleCollisionOnDelay )

    dr.object = self
    crate1.x = display.contentWidth/2
    crate1.y = display.contentHeight/2
  end
end

crate1.collision = onLocalCollision
crate1:addEventListener( "collision", crate1 )

1 ответ

Используйте свойство isBodyActive http://docs.coronalabs.com/api/type/Body/isBodyActive.html удачи

Другие вопросы по тегам