我迫切需要for循環的幫助。我試圖通過Corona SDK在Lua中進行for循環,但是我做錯了什麼,但我不知道是什麼。請參閱下面的我的代碼: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
當我拖放在屏幕上它已經登陸什麼樓層的電梯我想在這裏做的是檢查。我創建熱點(基本上hitboxes和目前擔任藝術佔位符),並把他們安置在熱點表是這樣的:
-- 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」,但在任何其他樓層,我收到消息:「hasCollided with floor:1」。所以我的moveLift函數中的for循環肯定會出錯,因爲它只返回第一層而不是其他任何樓層。
PS:正確的樓層是4層,頂層。
它是完全有道理的,它永遠不會超過1!從來沒有把回報視爲問題。移動回報,它就像一個魅力,thnx! – Tomjesch 2014-10-28 21:00:26
是的,返回的值是舊代碼的剩餘部分... – Tomjesch 2014-10-29 17:06:40