【问题标题】:Stop physics for single object in Corona SDK?在 Corona SDK 中停止单个对象的物理?
【发布时间】:2015-01-28 08:39:33
【问题描述】:

我在尝试重新启动游戏时遇到问题。我暂停物理,然后当用户单击按钮时,物理应该重新启动。当我输入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 )

【问题讨论】:

    标签: android lua coronasdk corona-storyboard


    【解决方案1】:

    使用 isBodyActive 属性 http://docs.coronalabs.com/api/type/Body/isBodyActive.html 祝你好运

    【讨论】:

    • 感谢链接,我试了一下,当我的对象 (crate1) 与某物碰撞时,它停止移动。不过,这并不完全是我想要完成的。板条箱必须能够击中东西让用户获得积分,但我希望它在击中屏幕边界之一时停止移动。我试图改变它,但我无法弄清楚它是如何工作的。我的代码如下。如果您有任何建议,请说出来。
    • 其实代码在上面。我编辑了我的问题,因为我无法将其放入评论中。
    • 设置 obj.gravityScale = 0 和 obj:setLinearVelocity(0,0);它停止了 obj
    • 我不认为我完全理解我将如何做到这一点。您介意在我上面的代码中显示我吗?我只是在努力学习。
    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多