Lua for循环不能正确迭代 [英] Lua for loop does not iterate properly
问题描述
pre code $ function $ move $ $ b $ j $ 1,4 $ do
if event.phase ==startedthen
markY = event.target.y
elseif event.phase ==movedthen
local y =(event .y - event.yStart)+ markY
event.target.y = y
elseif event.phase ==endedthen
if(hasCollided( event.target,hotSpots [j]))然后
print(hasCollided with floor:,hotSpots [j] .floor)
if(event.target.destination == hotSpots [j] .floor)then
print(correct floor)
succesfullPassengers = succesfullPassengers + 1
$ b如果succesfullPassengers == PASSENGER_AMOUNT then
print 游戏结束)
结束
其他
print(Wrong!电梯与地面相撞: ,hotSpots [j] .floor)
end
end
end
return true
end
end
我在这里要做的是检查何时在屏幕上拖放电梯,我已经创建了热点(基本上是hitbox,现在用作艺术占位符),并将它们放置在hotSpot表中,如下所示:
- - 创建电梯热点
pre>
为k = 1,4,1做
hotSpots [k] = display.newRect(gameAreaGroup,0,0,50,75)
hotSpots [k] .alpha = 0.25 - 使用alpha
显示热点hotpot [k] .floor = k - floor id
print(在地板上创建热点:,hotSpots [k] .floor)
hotSpots [k] .x = display.contentWidth * 0.5
hotSpots [k] .y = firstFloor - (FLOOR_HEIGHT * k)
hotSpots [k]:setFillColor(255,0,0)
热点[k]:addEventListener(tap,returnFloor) - 检查楼层值
gameAreaGroup:insert(hotSpots [k])
end
我用一个名为returnFloor的测试函数(1,2,3,4)检查每个热点是否有唯一的楼层值。当我把电梯拖到一楼时,我收到电梯与地板碰撞1:错误的信息,但在其他楼层,我收到有地板:1的信息。因此,我的moveLift函数中的for循环必须有错误,因为它只返回第一层而不是其他楼层。
PS:正确的楼层是4,
解决方案在for循环中有return true,所以它永远不会超过j = 1。我认为你可能需要在最后的if语句中或者在其后面的结尾之下移动这个语句(不知道完整的逻辑,我不确定返回的值用于什么)。
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.
解决方案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).
这篇关于Lua for循环不能正确迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!