改变函数中的变量

我想要在按下按钮时,一堆变量会更改。

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 = "牌堆\n价格:" .. 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 这些语句,但它仍然不起作用...

点赞
用户2196426
用户2196426

你通过值传递变量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中,您可以返回多个结果

2016-03-12 17:32:39