我尝试在场景中拖动重力 = 0,0 的动态主体,我有一个主体类型为动态的正方形,以及一个主体类型为静态的图像,但是当将方形拖动到图像上时,它会产生一点力,但是可以超出图像并传递到另一边,如图所示:
这是我拖动正方形的代码:
local function dragBody( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if "began" == phase then
stage:setFocus( body, event.id )
body.isFocus = true
body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
elseif body.isFocus then
if "moved" == phase then
body.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
stage:setFocus( body, nil )
body.isFocus = false
body.tempJoint:removeSelf()
end
end
return true
end
这是创建对象的代码:
function scene:createScene( event )
local group = self.view
my_square = display.newImage("square.png")
my_square.x = 60
my_square.y = 60
physics.addBody(my_square, "dynamic" )
group:insert(my_square)
floor = display.newImage("piso.png")
floor.x = 160
floor.y = 240
physics.addBody(floor, "static" )
group:insert(floor)
end
感谢帮助。
首先,我建议您尝试:
physics.setContinuous( false )
如果您已经这样做了:
Physics2D 引擎中有 3 种不同的物理类型。为了拖动目的,您可以使用“运动学”对象类型。但如果必须使用动态对象作为可拖动对象,则可能会出现碰撞错误。但如果你的静态对象每次都是一样的,你可以通过拖动功能来控制它。
我已经实现了一个小手机游戏,使用与你想要实现的相同的东西。链接在这里:https://itunes.apple.com/tr/app/bricks-world/id602065172?mt=8 https://itunes.apple.com/tr/app/bricks-world/id602065172?mt=8
如果您认为您想要在这个游戏中获得类似的东西,请发表评论^^我可以进一步提供帮助。
P.S:在游戏中,控制器桨是动态的,屏幕周围的墙壁是静态的。
另一个解决方案:
local lastX, lastY
local function dragBody( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if "began" == phase then
stage:setFocus( body, event.id )
body.isFocus = true
lastX, lastY = body.x, body.y
elseif body.isFocus then
if "moved" == phase then
-- You can change 1's with another value.
if(event.x > lastX) body.x = body.x + 1
else body.x = body.x - 1
if(event.y > lastY) body.y = body.y + 1
else body.y = body.y - 1
lastX, lastY = body.x, body.y
elseif "ended" == phase or "cancelled" == phase then
stage:setFocus( body, nil )
body.isFocus = false
end
end
return true
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)