Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua Corona SDk-如何让移动的对象旋转并移动到一个分接点?_Lua_Coronasdk - Fatal编程技术网

Lua Corona SDk-如何让移动的对象旋转并移动到一个分接点?

Lua Corona SDk-如何让移动的对象旋转并移动到一个分接点?,lua,coronasdk,Lua,Coronasdk,我对编程相当陌生,但我喜欢通过尝试和错误学习Corona(大部分是错误!) 我正在为一个大学项目创建一个空中交通管制Sim卡,在如何让一个物体(雷达上的飞机)转弯并到达一个分接点方面遇到了困难 我使用以下代码移动飞机: local function moveAirplane1(event) function cleanAirplaneRuntime() Runtime:removeEventListener("enterFrame", moveAirplane1) end if Airpla

我对编程相当陌生,但我喜欢通过尝试和错误学习Corona(大部分是错误!)

我正在为一个大学项目创建一个空中交通管制Sim卡,在如何让一个物体(雷达上的飞机)转弯并到达一个分接点方面遇到了困难

我使用以下代码移动飞机:

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)   
这很好,我可以通过添加或减去飞机旋转值来控制方向。然而,我想做的是允许玩家点击屏幕,让飞机旋转并移动到那个点

我一直在尝试计算攻丝点和飞机之间的角度,然后根据差值旋转飞机,但是根据飞机所在的象限以及攻丝点相对于飞机所在的象限,有太多的排列,我想知道我是否在正确的轨道上,还是有一个简单的方法这样做的方式,我错过了

这就是我一直在尝试的(抱歉,这是长篇大论)

-----------------------------------------------------------------
函数向量result()
函数圆(角度、精度)
返回数学楼层(角度介于*数学功率(10,0)+0.5)/数学功率(10,0)之间
结束
roundVector=圆形(角度、精度)
打印(“roundVector=“…roundVector”)
--象限1
如果飞机.x90和roundVector<180
newVector=180+圆向量
打印(“newVector=”…newVector)
埃尔塞夫
roundVector>180和newVector<270,然后
newVector=roundVector
打印(“newVector=”…newVector)
埃尔塞夫
圆形向量>270然后
newVector=180+圆向量
打印(“newVector=”…newVector)
结束
函数turnLeft1()
飞机旋转=飞机旋转-1
打印(“左转1”)
结束
函数turnTimer1()
--如果airplane.rotation~=airplaneHdg+newVector,则
turnLeftTimer1=计时器。性能带延迟(500,turnLeft1,newVector)
--结束
结束
交班时间1()
--结束
如果飞机.x90和roundVector<180
newVector=180+圆向量
打印(“newVector=”…newVector)
埃尔塞夫
roundVector>180和newVector<270,然后
newVector=roundVector
打印(“newVector=”…newVector)
埃尔塞夫
圆形向量>270然后
newVector=180+圆向量
打印(“newVector=”…newVector)
结束
函数turnRight1()
飞机旋转=飞机旋转+1
打印(“右转1”)
结束
函数turnTimer2()
如果飞机旋转~=180+newVector,则
turnOneRightTimer=timer.performWithDelay(500,turnRight1,newVector)
结束
结束
turnTimer2()
--象限2
elseif framework.x>vectorPoint.x和framework.y90和roundVector<180
newVector=180+圆向量
打印(“newVector=”…newVector)
埃尔塞夫
roundVector>180和newVector<270,然后
newVector=roundVector
打印(“newVector=”…newVector)
埃尔塞夫
圆形向量>270然后
newVector=180+圆向量
打印(“newVector=”…newVector)
结束
函数turnLeft2()
飞机旋转=飞机旋转-1
打印(“左转2”)
结束
函数turnTimer3()
如果airplane.rotation~=180-newVector,则
turnOneLeftTimer=timer.performWithDelay(500,turnLeft2,newVector)
结束
结束
turnTimer3()
elseif framework.x>vectorPoint.x和framework.y90和roundVector<180
newVector=180+圆向量
打印(“newVector=”…newVector)
埃尔塞夫
roundVector>180和newVector<270,然后
newVector=roundVector
打印(“newVector=”…newVector)
埃尔塞夫
圆形向量>270然后
newVector=180+圆向量
打印(“newVector=”…newVector)
结束
函数turnRight2()
飞机旋转=飞机旋转+1
打印(“右转2”)
结束
函数turnTimer4()
如果飞机旋转>180+新向量,则
turnOneRightTimer=定时器。延时性能(500,turnRight2,newVector)
结束
结束
turnTimer4()
--象限3
如果飞机.xvectorPoint.y
如果圆向量<90,则
newVector=90-roundVector+90
打印(“newVector=”…newVector)
埃尔塞夫
然后,roundVector>90和roundVector<180
newVector=180+圆向量
打印(“newVector=”…newVector)
埃尔塞夫
圆维托
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
local W = display.contentWidth
local H = display.contentHeight


local bg = display.newRect(0,0, W,H)
local img = display.newImage("triangle.png")--make an image arrow pointing to the right
img.x = W/2
img.y = H/2

local getX
local getY
local travelFlag = true


function getImageRotation(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 function onTravel()

    local function Flag()
        travelFlag = true
    end

    if travelFlag then
        travelFlag = false
        img.rotation = (getImageRotation(img.x,img.y,getX,getY))*-1
        transition.to( img, { time=2000, x=getX, y=getY, onComplete=Flag } )
    end
end

local function onTap(event)

    if event.phase == "began" or event.phase == "moved" then
        getX = event.x
        getY = event.y

    elseif event.phase == "ended" then

        onTravel()
    end

end
Runtime:addEventListener("touch", onTap)
local W = display.contentWidth
local H = display.contentHeight


local bg = display.newRect(0,0, W,H)
local img = display.newImage("triangle.png")--make an image arrow pointing to the right
img.x = W/2
img.y = H/2

local getX
local getY
local travelFlag = true

local distanceX
local distanceY
local totalDistance

function getImageRotation(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 function onTravel()

distanceX=img.x-getX
distanceY=img.y-getY

print(distanceX)
print(distanceY)

totalDistance=(distanceX*distanceX)+(distanceY*distanceY)
totalDistance=math.sqrt(totalDistance)
print(totalDistance)

    local function Flag()
        travelFlag = true
    end

    if travelFlag then
        travelFlag = false
        img.rotation = (getImageRotation(img.x,img.y,getX,getY))*-1
        transition.to( img, { time=totalDistance*5, x=getX, y=getY, onComplete=Flag } )
    end
end

local function onTap(event)

    if event.phase == "began" or event.phase == "moved" then
        getX = event.x
        getY = event.y

    elseif event.phase == "ended" then

        onTravel()
    end

end
Runtime:addEventListener("touch", onTap)