2015-01-10 5 views
0

마우스 오른쪽 버튼을 누르면 "모드"테이블에 두 번째 문자열을 인쇄하려고했습니다. 그러나 버튼을 클릭하면 "모드 : 원"대신 "모드 : 1"이 인쇄됩니다. 여기 내 코드는 다음과 같습니다.인쇄 된 문자열을 Lua, Love2D의 테이블로 대체하는 방법

function love.load() 
mode = {"square", "circle"} 
currentMode = mode[1] 
end 

function nextMode() 
currentMode = next(mode) 
print(currentMode) 
end 

function love.draw() 
love.graphics.print("mode: " .. currentMode, 10, 10) 
end 

function love.mousepressed(x, y, button) 
if button == "r" then 
nextMode() 
end 
end 

누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까?

답변

2

next은 색인 및 값을 반환하지만 상태를 보유하지 않으므로 이전 색인을 전달하는 두 번째 인수가 있습니다. 하여 예에서 nextIndex, nextValue = next(mytable, previousIndex)

일반

하여 currentMode "원"의 인덱스이고, 여기서 2

이 실시 예의 한 값이다 nextIndex 할당되고 :

function love.load() 
    mode = {"square", "circle"} 
    currentIndex, currentMode = next(mode) 
end 


function love.mousepressed(x, y, button) 
    if button == "r" then 
     currentIndex, currentMode = next(mode, currentIndex) 
    end 
end 

function love.draw() 
    love.graphics.print("mode: "..currentMode, 10, 10) 
end