Lua 沿直线路径将对象移动到目标位置
我又面临一个问题。所以,我在科罗纳做一个游戏。我希望物体沿直线移动到触摸坐标。我知道我可以简单地使用Lua 沿直线路径将对象移动到目标位置,lua,coronasdk,Lua,Coronasdk,我又面临一个问题。所以,我在科罗纳做一个游戏。我希望物体沿直线移动到触摸坐标。我知道我可以简单地使用transition.to()函数,但是物理引擎在转换期间不能正常工作。我编写了下面的代码,但当然,圆不会沿着直线移动 function controls(event) if event.phase == "began" or event.phase == "moved" then follow = true touchX = event.x; touchY
transition.to()
函数,但是物理引擎在转换期间不能正常工作。我编写了下面的代码,但当然,圆不会沿着直线移动
function controls(event)
if event.phase == "began" or event.phase == "moved" then
follow = true
touchX = event.x; touchY = event.y
end
if event.phase == "ended" then
follow = false
end
end
function playerMotion()
if follow == true then
if circle.y < touchY then
circle.y = circle.y + 1
elseif circle.y > touchY then
circle.y = circle.y - 1
end
if circle.x < touchX then
circle.x = circle.x + 1
elseif circle.x > touchX then
circle.x = circle.x - 1
end
end
end
功能控制(事件)
如果event.phase==“开始”或event.phase==“移动”,则
follow=true
touchX=event.x;touchY=event.y
结束
如果event.phase==“结束”,则
follow=false
结束
结束
函数playerMotion()
如果follow==true,则
如果circle.y那么敏感吗
circle.y=circle.y-1
结束
如果圆.x然后触摸x
圆圈.x=圆圈.x-1
结束
结束
结束
希望我的问题足够清楚。您需要比这个函数更复杂的东西。这只是“接近”目的地。它不走直线 要完成你想做的事情,你需要做几件事:
给你 试试我的示例应用程序。你可以使用它,或者为你的项目想出一个主意 您还可以在空白项目中对此进行测试,以了解其工作原理
_W = display.contentWidth
_H = display.contentHeight
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
local circle = display.newCircle(0,0,20)
circle.name = "circle"
circle.x = _W/2
circle.y = _H/2
circle.tx = 0
circle.ty = 0
physics.addBody(circle)
circle.linearDamping = 0
circle.enterFrame = function(self,event)
print(self.x,self.tx)
--This condition will stop the circle on touch coordinates
--You can change the area value, this will detect if the circles's x and y is between the circles's tx and ty
--If area is 0, it may not stop the circle, area = 5 is safe, change if you want to
local area = 5
if self.x <= self.tx + area and self.x >= self.tx - area and
self.y <= self.ty + area and self.y >= self.ty - area then
circle:setLinearVelocity(0,0) --set velocity to (0,0) to fully stop the circle
end
end
--Add event listener for the monitoring the coordinates of the circle
Runtime:addEventListener("enterFrame",circle)
Runtime:addEventListener("touch",function(event)
if event.phase == "began" or event.phase == "moved" then
local x, y = event.x, event.y
local tx, ty = x-circle.x, y-circle.y --compute for the toX and toY coordinates
local sppedMultiplier = 1.5 --this will change the speed of the circle, 0 value will stop the circle
--sets the future destination of the circle
circle.tx = x
circle.ty = y
circle:setLinearVelocity(tx*delay,ty*delay) --this will set the velocity of the circle towards the computed touch coordinates on a straight path.
end
end)
\u W=display.contentWidth
_H=显示高度
本地物理=要求(“物理”)
物理开始
物理学.重力(0,0)
本地圆=显示。新圆(0,0,20)
circle.name=“circle”
圆x=_W/2
圆y=_H/2
circle.tx=0
圆0.ty=0
物理。addBody(圆)
circle.linearDamping=0
circle.enterFrame=函数(自身、事件)
打印(self.x、self.tx)
--此情况将停止接触坐标上的圆
--您可以更改面积值,这将检测圆的x和y是否在圆的tx和ty之间
--如果面积为0,则可能无法停止圆,面积=5是安全的,如果需要,请进行更改
局部面积=5
如果self.x=self.tx-区域和
self.y=self.ty-面积
圆:setLinearVelocity(0,0)--将速度设置为(0,0)以完全停止圆
结束
结束
--添加用于监视圆坐标的事件侦听器
运行时:addEventListener(“enterFrame”,圆圈)
运行时:addEventListener(“触摸”),函数(事件)
如果event.phase==“开始”或event.phase==“移动”,则
本地x,y=event.x,event.y
局部tx,ty=x-circle.x,y-circle.y——计算toX和toY坐标
本地sppedMultiplier=1.5——这将改变圆的速度,0值将停止圆
--设置圆的未来目标
circle.tx=x
圆y=y
圆:setLinearVelocity(tx*延迟,ty*延迟)——这将设置圆在直线路径上朝向计算的触摸坐标的速度。
结束
(完)
这是将圆移动到接触点的数学:
theta = atan2(touchY - circle.y,touchX - circle.x)
velx = cos(theta)
vely = sin(theta)
circle.x += velx
circle.y += vely
当圆接近接触点时,速度将接近0。复合赋值运算符
+=
在Lua中不存在;我假设这是某种伪代码。速度不会接近零,但将始终为1,因为theta
将保持不变(如果不需要单位速度,则应通过某个速度系数缩放velx
和vely
)。圆圈
位置永远不会完全到达触摸
位置,而是在触摸
位置附近摆动结束。这在实践中可能是好的,但看起来可能会紧张。如果您希望圆停止,则必须在圆“接近”接触点时停止。