如何在Corona SDK中停止单个对象的物理效果?

我想重启游戏时遇到了问题。我暂停了物理引擎,然后当用户点击按钮时,物理引擎应该重新启动。但是,当我添加了 physics.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 )
点赞
用户1376249
用户1376249

使用 isBodyActive 属性

http://docs.coronalabs.com/api/type/Body/isBodyActive.html

祝你好运

2014-11-30 06:13:19