2015-02-24 71 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 })  

我已經嘗試過這在仿真器中,並將其作品。