【发布时间】:2016-11-20 23:59:04
【问题描述】:
我正在使用this simple virtual joystick module,我试图让我的播放器根据操纵杆的角度在 360 度方向上旋转,但它不能正常工作。
这是模块中最相关的代码:
local radToDeg = 180/math.pi
local degToRad = math.pi/180
-- where should joystick motion be stopped?
local stopRadius = outerRadius - innerRadius
local directionId = 0
local angle = 0
local distance = 0
function joystick:touch(event)
local phase = event.phase
if( (phase=='began') or (phase=="moved") ) then
if( phase == 'began' ) then
stage:setFocus(event.target, event.id)
end
local parent = self.parent
local posX, posY = parent:contentToLocal(event.x, event.y)
angle = (math.atan2( posX, posY )*radToDeg)-90
if( angle < 0 ) then
angle = 360 + angle
end
-- could expand to include more directions (e.g. 45-deg)
if( (angle>=45) and (angle<135) ) then
directionId = 2
elseif( (angle>=135) and (angle<225) ) then
directionId = 3
elseif( (angle>=225) and (angle<315) ) then
directionId = 4
else
directionId = 1
end
distance = math.sqrt((posX*posX)+(posY*posY))
if( distance >= stopRadius ) then
distance = stopRadius
local radAngle = angle*degToRad
self.x = distance*math.cos(radAngle)
self.y = -distance*math.sin(radAngle)
else
self.x = posX
self.y = posY
end
else
self.x = 0
self.y = 0
stage:setFocus(nil, event.id)
directionId = 0
angle = 0
distance = 0
end
return true
end
function joyGroup:getAngle()
return angle
end
以下是我在设置操纵杆后尝试移动播放器的方法:
local angle = joyStick.getAngle()
player.rotation = angle
angle 和 player.rotation 具有完全相同的值,但玩家的旋转方向与操纵杆不同,因为操纵杆的默认 0 旋转方向是正确的方向(东)并且是逆时针方向。
【问题讨论】:
标签: lua rotation coronasdk joystick