Lua 旋转与中心相关的对象
事情是这样的,我在游戏中有一门大炮,我想用手指转动它。我希望尖端始终指向手指拖动到的相反方向,即如果手指拖动到左下角,尖端应指向右上角 我就是想不出如何让它发挥作用。我已使用以下代码使其旋转:Lua 旋转与中心相关的对象,lua,coronasdk,Lua,Coronasdk,事情是这样的,我在游戏中有一门大炮,我想用手指转动它。我希望尖端始终指向手指拖动到的相反方向,即如果手指拖动到左下角,尖端应指向右上角 我就是想不出如何让它发挥作用。我已使用以下代码使其旋转: local function rotateObj(event) local t = event.target local phase = event.phase if (phase == "began") then display.getCurrentStage(
local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == "began") then
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position of finger
t.x1 = event.x
t.y1 = event.y
elseif t.isFocus then
if (phase == "moved") then
t.x2 = event.x
t.y2 = event.y
angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
print("angle1 = "..angle1)
rotationAmt = angle1 - angle2
--rotate it
t.rotation = t.rotation - rotationAmt
print ("t.rotation = "..t.rotation)
t.x1 = t.x2
t.y1 = t.y2
elseif (phase == "ended") then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
-- Stop further propagation of touch event
return true
end
cannon:addEventListener("touch", rotateObj)
虽然这允许我旋转我的大炮,但它并没有保持顶端与我拖动的位置的关系。我甚至不知道从这里到哪里去。你可以试试我的代码
我也创造了一个像你一样的游戏,但你可以控制弓箭
function getAngle(x1,y1,x2,y2)
local PI = 3.14159265358
local deltaY = y2 - y1
local deltaX = x2 - x1
local angleInDegrees = (math.atan2( deltaY, deltaX) * 180 / PI)*-1
local mult = 10^0
return math.floor(angleInDegrees * mult + 0.5) / mult
end
local arrow = display.newImage("wind_arrow.png")
arrow.x = display.contentWidth/2
arrow.y = display.contentHeight/2
arrow.touch = function(self, event)
print(event.phase)
if event.phase == "moved" then
--If your image is originally pointing to the north use +90
--If your image is originally pointing to the east use +180
--If your image is originally pointing to the south use +270
--If your image is originally pointing to the west use +360 or +0
--My wind_arrow.png is point to the east so I use +180
--You can use this formula
arrow.rotation = (getAngle(arrow.x,arrow.y,event.x,event.y)+180)*-1
end
end
Runtime:addEventListener("touch",arrow)
你可以试试我的代码
我也创造了一个像你一样的游戏,但你可以控制弓箭
function getAngle(x1,y1,x2,y2)
local PI = 3.14159265358
local deltaY = y2 - y1
local deltaX = x2 - x1
local angleInDegrees = (math.atan2( deltaY, deltaX) * 180 / PI)*-1
local mult = 10^0
return math.floor(angleInDegrees * mult + 0.5) / mult
end
local arrow = display.newImage("wind_arrow.png")
arrow.x = display.contentWidth/2
arrow.y = display.contentHeight/2
arrow.touch = function(self, event)
print(event.phase)
if event.phase == "moved" then
--If your image is originally pointing to the north use +90
--If your image is originally pointing to the east use +180
--If your image is originally pointing to the south use +270
--If your image is originally pointing to the west use +360 or +0
--My wind_arrow.png is point to the east so I use +180
--You can use this formula
arrow.rotation = (getAngle(arrow.x,arrow.y,event.x,event.y)+180)*-1
end
end
Runtime:addEventListener("touch",arrow)
看看你的第一句话
if (phase == "began") then
t.isFocus = true
elseif t.isFocus then
XXX
end
当phase==“开始”时,它将不起作用,因为如果elseif语句将进入一条路径。
在if中,您将t.isFocus设置为true,但不会立即在elseif路径中对其进行判断。查看您的第一个if-elseif语句
if (phase == "began") then
t.isFocus = true
elseif t.isFocus then
XXX
end
当phase==“开始”时,它将不起作用,因为如果elseif语句将进入一条路径。
在if中,您将t.isFocus设置为true,但不会立即在elseif路径中进行判断。谢谢!这正是我想要的。这段代码不仅比我原来的代码小很多,而且非常容易使用。非常感谢!事实上,我注意到一个小错误。。。现在,每当我触摸屏幕时,大炮就会移动,而不仅仅是当我触摸大炮时。只有当用户触摸大炮时,我才能让它工作吗?我真的应该先自己动手弄清楚,然后再问问题。通过将运行时事件侦听器更改为:cannon:addEventListener(“touch”,cannon),可以很容易地解决这个问题。谢谢!这正是我想要的。这段代码不仅比我原来的代码小很多,而且非常容易使用。非常感谢!事实上,我注意到一个小错误。。。现在,每当我触摸屏幕时,大炮就会移动,而不仅仅是当我触摸大炮时。只有当用户触摸大炮时,我才能让它工作吗?我真的应该先自己动手弄清楚,然后再问问题。通过将运行时事件侦听器更改为:cannon:addEventListener(“touch”,cannon),可以很容易地解决这个问题