Lua 360度虚拟操纵杆旋转
我用的是,我试着让我的播放器根据操纵杆的角度360度旋转,但它不能正常工作 以下是模块中最相关的代码:Lua 360度虚拟操纵杆旋转,lua,rotation,coronasdk,joystick,Lua,Rotation,Coronasdk,Joystick,我用的是,我试着让我的播放器根据操纵杆的角度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 dista
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
angle
和player.rotation
具有完全相同的值,但播放机的旋转方向与操纵杆不同,因为操纵杆默认的0旋转方向是向右(东)的,并且是逆时针方向。请尝试player.rotation=-angle
。播放器
和操纵杆
应朝同一方向旋转
使用simpleJoystick模块,您可以获得(度)
北-90
西-180
东-0/360
南-270
如果你想得到
北-0
西-90
东-270
南-180
像这样修改simpleJoystick模块中的代码
...
angle = (math.atan2( posX, posY )*radToDeg)-180
...
self.x = distance*math.cos(radAngle + 90*degToRad)
self.y = -distance*math.sin(radAngle + 90*degToRad)
...
...
angle = (math.atan2( posX, posY )*radToDeg)-180
...
self.x = distance*math.cos(radAngle + 90*degToRad)
self.y = -distance*math.sin(radAngle + 90*degToRad)
...