2015-02-24 70 views
0

我刚开始制作我的第一个Corona游戏,并且已经遇到问题。在下面的代码中,箱子应该从用户绘制的线上弹开,但由于某种原因,箱子只是继续移动,就好像线条不在那里一样。如果有人能帮助我,我将不胜感激。Corona SDK中的身体没有响应

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) 
+0

我得到一个错误,说“电晕不能索引几乎没有区域的身体”。尽管我在2013版的corona上试过这个代码,但它工作正常。有什么建议? – felix 2015-02-24 13:49:24

回答

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

删除:形状= {0,0,dist_x,dist_y,0,0}

更改为:

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

我已经尝试过这在仿真器中,并将其作品。