物理 body 在 Corona SDK 中没有响应
Physical body not responding in 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)
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 } )
我已经在模拟器中试过了,效果很好。
我刚开始制作我的第一个 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)
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 } )
我已经在模拟器中试过了,效果很好。