当我按下一个按钮,一堆变量改变的时候,我想做它。
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之类的,但还是没用.
发布于 2016-03-12 17:32:39
通过值传递变量price,而不是传递以参考。这个构造在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你可以返回多个结果。
https://stackoverflow.com/questions/35960681
复制相似问题