身体在Corona SDK中无响应

时间:2015-02-24 01:52:30

标签: lua box2d corona

我刚刚开始制作我的第一个Corona游戏,并且已经遇到了问题。在下面的代码中,crate应该从用户绘制的线条反弹,但由于某种原因,crate只是继续移动,好像线条不在那里。如果有人能帮助我,我会非常感激。

 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)

1 个答案:

答案 0 :(得分:0)

         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 } )    

我已经在模拟器中尝试了这个并且它可以工作。