物理身体在Corona SDK中无响应

我刚刚开始制作我的第一个 Corona 游戏,并已经遇到了问题。在下面的代码中,箱子应该弹回用户画的线,但是由于某种原因,箱子仍然在移动,就好像线根本不存在一样。如果有人能帮我解决这个问题,我会非常感激的。

 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)

local line
lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 0

local function distanceBetween(x1, y1, x2, y2)
   local dist_x = x2 - x1
   local dist_y = y2 - y1
   local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
   return distanceBetween
end

local function drawLine(e)
if(e.phase == "began") then
  if(line) then
    lineGroup:remove(1)
    line = nil
  end

  prevX = e.x
  prevY = e.y
  isDrawing = true
elseif(e.phase == "moved") then
  local distance = distanceBetween(prevX, prevY, e.x, e.y)
  if(isDrawing and distance < 100) then
     if(line) then lineGroup:remove(1) end
     line = display.newLine(prevX, prevY, e.x, e.y)
     line:setStrokeColor( 0.5,0,1 )
     line.strokeWidth = 5

    local dist_x = e.x - prevX
    local dist_y = e.y - prevY
    physics.addBody(line, "static", { density = 1, friction = 0.5, bounce     = 2, shape = {0,0, dist_x, dist_y, 0, 0} } )
    lineGroup:insert(line)
 end
elseif(e.phase == "ended") then
  isDrawing = false
 end
end

Runtime:addEventListener("touch",drawLine)
点赞
用户4599324
用户4599324
将下面翻译成中文并且保留原本的 markdown 格式:

physics.addBody(line, "static", { density = 1, friction = 0.5, bounce = 2, shape = {0,0, dist_x, dist_y, 0, 0} } )


删除:`shape = {0,0, dist_x, dist_y, 0, 0}`

更改为:

physics.addBody(line, "static", { density = 1, friction = 0.5, bounce = 2 } )

```

我已经在模拟器上尝试了这个方法并且有效。

2015-02-27 02:42:10