我遇到了可拖动对象的问题。当我将对象快速拖动到另一个对象时,它会粘在另一个对象内部。然后,当我释放它时,它会射击。
这是我的代码,我用gameUI.lua编写。有人可以帮助我吗?
display.setStatusBar(display.HiddenStatusBar)
local physics = require("physics")
physics.start()
physics.setDrawMode("normal")
physics.setGravity(0,5)
local gameUI = require("gameUI")
local floor = display.newImage("floor.jpg")
physics.addBody(floor, "static", {friction = 999, bounce = 0})
floor.y = 900
local box = display.newImage("box.png")
physics.addBody(box, {friction = 99, bounce = 0})
box.x = 100
function localDrag( event )
gameUI.dragBody( event, { maxForce=100000, frequency=1000, dampingRatio=1, center=true })
end
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
self.markY = self.y
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y
if (self.x > 595) then
self.x = 595
end
if (self.x < 46 ) then
self.x = 46
end
if (self.y > 795) then
self.y = 795
end
end
return true
end
box:addEventListener( "touch", localDrag)
local triangle = display.newImage("triangle.png")
triangle.x = 200
triangleShape = { -45, 45 , -45, -45 , 45, 45 }
physics.addBody(triangle, {friction = 99, bounce = 0, shape=triangleShape})
function localDrag( event )
gameUI.dragBody( event, { maxForce=1000000, frequency=1000, dampingRatio=1, center=true })
end
function triangle:touch( event )
if event.phase == "began" then
self.markX = self.x
self.markY = self.y
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y
if (self.x > 595) then
self.x = 595
end
if (self.x < 46 ) then
self.x = 46
end
if (self.y > 795) then
self.y = 795
end
end
return true
end
triangle:addEventListener( "touch", localDrag)
local wall = display.newImage("wall.jpg")
physics.addBody(wall, "static", {friction = 0, bounce = 0})
wall.x = 665
local wall1 = display.newImage("wall.jpg")
physics.addBody(wall1, "static", {friction = 0, bounce = 0})
wall1.x = -25
答案 0 :(得分:1)
你的摩擦非常非常高,我建议减少很多。 (在Corona SDK文档中阅读有关身体属性的内容可能没有什么坏处,它的详细信息远远超过我在这里发布的一篇文章。)
对于你的身体卡住,这可能与摩擦有关,虽然如果它们实际上是重叠的,我会怀疑这是因为睡觉。您可以将obj.isSleepingAllowed = false设置为导致问题的实体并测试,我相信您可能会看到明显的改进。