2016-03-12 63 views
1

我想在按下按钮时使一堆变量发生变化。更改函数中的变量

function BuyItem(price, quantity, pps, text, quantitytext) 
    if(PixoosQuantity >= price) then 
     PixoosQuantity = PixoosQuantity - price 
     price = price * 1.1 

     quantity = quantity + 1 

     PixoosPerSecond = PixoosPerSecond + pps 
     PixoosPerSecondDisplay.text = "PPS: " .. string.format("%.3f", PixoosPerSecond) 
     PixoosQuantityDisplay.text = "Pixoos: " .. string.format("%.3f", PixoosQuantity) 

     text.text = "Deck of playing cards\nPrice: " .. string.format("%.3f", price) .. " Pixoos" 
     quantitytext.text = quantity 
    end 
end 

这是其能够顺利通过按下按钮调用的函数:

function ButtonAction(event) 
    if event.target.name == "DeckOfPlayingCards" then 
     BuyItem(DeckOfPlayingCardsPrice, DeckOfPlayingCardsQuantity, DeckOfPlayingCardsPPS, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText) 
    end 
end 

我的问题是,为什么不变量的变化?我试图把return price等,但它仍然无法正常工作......

回答

1

您传递变量price按价值计算,不by reference。这种结构不存在在Lua,所以你需要使用的返回值要解决它,例如:

DeckOfPlayingCardsPrice, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText = BuyItem(DeckOfPlayingCardsPrice, [...], DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText) 

和正确返回预期值:

function BuyItem(price, quantity, pps, text, quantitytext) 
    if(PixoosQuantity >= price) then 
     [...] 
    end 
    return price, quantity, quantitytext 
end 

在Lua中,你可以return multiple results

+0

问题是我不仅需要更改价格,还需要更改数量,文本和数量文本。我怎样才能在一个功能中完成所有功能?甚至有可能吗? – FICHEKK

+0

在Lua中,您可以返回更多值并将其分配给适当的变量。我修改了答案。 – Jakuje

+0

你是一个兄弟!谢谢你,兄弟!这解决了它! – FICHEKK