プログラミング初心者ですが、試行錯誤しながら楽しくコロナを学んでいます(ほとんどが間違い!)
私は大学のプロジェクト用に航空交通管制シミュレーションの作成に取り組んでおり、オブジェクト (レーダー上の飛行機) を回転させてタップ ポイントに移動させる方法についてレンガの壁にぶつかりました。
次のコードを使用して飛行機を動かしています。
local function moveAirplane1(event)
function cleanAirplaneRuntime()
Runtime:removeEventListener("enterFrame", moveAirplane1)
end
if Airplane1OnRadar == false then
cleanEI137Runtime()
end
Airplane1.x = Airplane1.x + math.cos(math.rad(Airplane1.rotation)) * Airplane1SPEED
Airplane1.y = Airplane1.y + math.sin(math.rad(Airplane1.rotation)) * Airplane1SPEED
end
Runtime:addEventListener("enterFrame", moveAirplane1)
これは問題なく動作し、plane.rotation の値を加算または減算することで方向を制御できます。ただし、私がやりたいことは、プレイヤーが画面をタップして飛行機を回転させ、その地点まで移動できるようにすることです。
私はタップポイントと飛行機の間の角度を計算し、その差で航空機を回転させようとしましたが、飛行機がどの象限にあるか、およびタップポイントが飛行機に対してどの象限にあるかに応じて、非常に多くの順列があります。私は正しい軌道に乗っているのか、それとも行方不明になっている簡単な方法があるのか 疑問に思っていますか?
これが私が試してきたことです(申し訳ありませんが、長くなりました)
function vectorTo()
function setVectorPoint(event)
vectorPoint = display.newCircle(0, 0, 5)
vectorPoint.x = event.x
vectorPoint.y = event.y
vectorPoint.alpha = 0.5
airplane = EI159
vP = vectorPoint
airplaneHdg = EI159CurrentHeading
Runtime:removeEventListener("tap", setVectorPoint)
function angleBetween(vP, airplane ) -- Calculate angle between airplane and tap point
airplane = Airplane1
vP = vectorPoint
xDist = airplane.x - vP.x
yDist = airplane.y - vP.y
angleBetween = math.deg( math.atan( yDist/xDist ) )
return angleBetween
end
-------------------------------------------------- ---------------
function vectorResult()
function round(angleBetween, precision)
return math.floor(angleBetween*math.pow(10,0)+0.5) / math.pow(10,0)
end
roundVector = round(angleBetween,precision)
print("roundVector = "..roundVector)
--Quadrant 1
if airplane.x < vP.x and airplane.y < vP.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnLeft1()
airplane.rotation = airplane.rotation - 1
print("Turn Left1")
end
function turnTimer1()
--if airplane.rotation ~= airplaneHdg + newVector then
turnLeftTimer1 = timer.performWithDelay(500, turnLeft1, newVector)
--end
end
turnTimer1()
--end
elseif airplane.x < vP.x and airplane.y < vP.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnRight1()
airplane.rotation = airplane.rotation + 1
print("Turn Right1")
end
function turnTimer2()
if airplane.rotation ~= 180 + newVector then
turnOneRightTimer = timer.performWithDelay(500, turnRight1,newVector)
end
end
turnTimer2()
--Quadrant 2
elseif airplane.x > vectorPoint.x and airplane.y < vectorPoint.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnLeft2()
airplane.rotation = airplane.rotation - 1
print("Turn Left2")
end
function turnTimer3()
if airplane.rotation ~= 180 - newVector then
turnOneLeftTimer = timer.performWithDelay(500, turnLeft2, newVector)
end
end
turnTimer3()
elseif airplane.x > vectorPoint.x and airplane.y < vectorPoint.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnRight2()
airplane.rotation = airplane.rotation + 1
print("Turn Right2")
end
function turnTimer4()
if airplane.rotation > 180 + newVector then
turnOneRightTimer = timer.performWithDelay(500, turnRight2, newVector)
end
end
turnTimer4()
--Quadrant 3
elseif airplane.x < vectorPoint.x and airplane.y > vectorPoint.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnLeft3()
airplane.rotation = airplane.rotation - 1
print("Turn Left3")
end
function turnTimer5()
if airplane.rotation < 180 - newVector then
turnOneLeftTimer = timer.performWithDelay(500, turnLeft3, newVector)
end
end
turnTimer5()
elseif airplane.x < vectorPoint.x and airplane.y > vectorPoint.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnRight3()
airplane.rotation = airplane.rotation + 1
print("Turn Right3")
end
function turnTimer6()
if airplane.rotation > 180 - newVector then
turnOneRightTimer = timer.performWithDelay(500, turnRight3, newVector)
end
end
turnTimer6()
--Quadrant 4
elseif airplane.x > vectorPoint.x and airplane.y > vectorPoint.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnRight4()
airplane.rotation = airplane.rotation + 1
print("Turn Right4")
end
function turnTimer7()
if airplane.rotation ~= newVector then
turnTimer = timer.performWithDelay(500, turnRight4, newVector)
end
end
turnTimer7()
elseif airplane.x > vectorPoint.x and airplane.y > vectorPoint.y then
if roundVector < 90 then
newVector = 90 - roundVector + 90
print("newVector = "..newVector)
elseif
roundVector > 90 and roundVector < 180 then
newVector = 180 + roundVector
print("newVector = "..newVector)
elseif
roundVector > 180 and newVector < 270 then
newVector = roundVector
print("newVector = "..newVector)
elseif
roundVector > 270 then
newVector = 180 + roundVector
print("newVector = "..newVector)
end
function turnLeft4()
airplane.rotation = airplane.rotation - 1
print("Turn Left4")
end
function turnTimer8()
if airplane.rotation ~= newVector then
turnTimer = timer.performWithDelay(500, turnLeft4, newVector)
end
end
turnTimer8()
end
end
vectorResulttimer = timer.performWithDelay(800, vectorResult)
function removeVP()
display.remove(vectorPoint)
vectorPoint = nil
Airplane1dA.isVisible = false
timer.cancel(removeVPTimer)
end
removeVPTimer = timer.performWithDelay(1500, removeVP)
timer.performWithDelay(600, angleBetween)
end
function addEventVectorPoint()
Runtime:addEventListener("tap", setVectorPoint)
end
vectorPointTimer = timer.performWithDelay(500, addEventVectorPoint)
end
事前に助けや指針をありがとう、