Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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
For loop Lua for循环不能正确迭代_For Loop_Lua_Drag And Drop_Coronasdk_Collision - Fatal编程技术网

For loop Lua for循环不能正确迭代

For loop Lua for循环不能正确迭代,for-loop,lua,drag-and-drop,coronasdk,collision,For Loop,Lua,Drag And Drop,Coronasdk,Collision,我急需一个for循环的帮助。我试图用Corona SDK在Lua中进行for循环,但我做错了什么,但我不知道是什么。请参阅下面的代码: function moveLift(event) for j=1,4,1 do if event.phase == "began" then markY = event.target.y elseif event.phase == "moved" then local y

我急需一个for循环的帮助。我试图用Corona SDK在Lua中进行for循环,但我做错了什么,但我不知道是什么。请参阅下面的代码:

function moveLift(event)
    for j=1,4,1 do
        if event.phase == "began" then
            markY = event.target.y

        elseif event.phase == "moved" then
            local y = (event.y - event.yStart) + markY
            event.target.y = y

        elseif event.phase == "ended" then

            if (hasCollided( event.target, hotSpots[j] )) then

                print("hasCollided with floor: ", hotSpots[j].floor)

                if (event.target.destination == hotSpots[j].floor) then
                    print("correct floor")
                    succesfullPassengers = succesfullPassengers + 1

                    if succesfullPassengers == PASSENGER_AMOUNT then
                        print("game over")
                    end
                else
                    print("Wrong! elevator has collided with floor: ", hotSpots[j].floor)
                end
            end
        end
        return true
    end
end
我在这里要做的是检查当我在屏幕上拖放电梯时,它降落在哪一层。我已经创建了热点(基本上是Hitbox,目前用作艺术占位符),并将它们放置在热点表中,如下所示:

-- Create elevator hotspots
for k=1,4,1 do
    hotSpots[k] = display.newRect( gameAreaGroup, 0, 0, 50, 75 )
    hotSpots[k].alpha = 0.25  --Show hotspots with alpha
    hotSpots[k].floor = k -- The floor id
    print("Created hotspot on floor: ",hotSpots[k].floor)
    hotSpots[k].x = display.contentWidth *0.5
    hotSpots[k].y = firstFloor - (FLOOR_HEIGHT * k)
    hotSpots[k]:setFillColor( 255,0,0 )
    hotSpots[k]:addEventListener( "tap", returnFloor ) -- Check floor value
    gameAreaGroup:insert(hotSpots[k])
end
我使用一个名为returnFloor的测试函数检查每个热点是否有唯一的地板值,它们有(1,2,3,4)。当我在一楼拖放我的电梯时,我会收到信息“错误!电梯与楼层:1发生碰撞”,但在任何其他楼层,我都会收到信息:“已与楼层:1发生碰撞”。所以我的moveLift函数中的for循环肯定有问题,因为它只返回1层,而不返回任何其他层


注:正确的楼层是4,顶层。

在for循环中有“return true”,因此它永远不会超过j=1。我认为您可能需要将该语句上移到最后一个if语句中,或者移到它后面的“end”下面(不知道完整的逻辑,我不确定返回值的用途)。

最后一行代码不应该是
end return true end

end-end-return-true-end
因此,返回是在循环完成之后进行的。

完全可以理解,它永远不会超过1!从未将回报视为问题所在。把回音调高了,它就像一个魔咒一样工作,thnx!