Lua for loop does not iterate properly

陌路散爱 提交于 2020-01-15 12:33:23

问题


I am in dire need of help with a for loop. I'm trying to go through a for loop in Lua with the Corona SDK, but I am doing something wrong but I don't know what. See below for my code:

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

What I'm trying to do here is checking when I drag and drop an elevator on screen on what floor it has landed. I've created hotspots (basically hitboxes and currently serving as art placeholder) and placed them in the hotSpot table like this:

-- 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

I check if every hotspot has a unique floor value with a test function called returnFloor, which they have (1,2,3,4). When I drag and drop my elevator on the first floor, I receive the message "Wrong! elevator has collided with floor: 1", but on any other floor I receive the message: "hasCollided with floor: 1". So there must be something wrong with the for loop in my moveLift function because it only returns floor 1 and not any other floor.

PS: the correct floor is 4, the top floor.


回答1:


You have "return true" inside your for loop, so it will never get past j=1. I think you may need to move that statement up inside the last if statement, or below the "end" that follows it (without knowing the full logic, I'm not sure what the returned value is used for).




回答2:


Last lines of code should not be end end return true end end but end end end return true end so the return is after the loop has completed.



来源:https://stackoverflow.com/questions/26615777/lua-for-loop-does-not-iterate-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!