首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >变函数变量

变函数变量
EN

Stack Overflow用户
提问于 2016-03-12 17:06:11
回答 1查看 68关注 0票数 1

当我按下一个按钮,一堆变量改变的时候,我想做它。

代码语言:javascript
复制
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

这是一个在按下按钮时调用的函数:

代码语言:javascript
复制
function ButtonAction(event)
    if event.target.name == "DeckOfPlayingCards" then
        BuyItem(DeckOfPlayingCardsPrice, DeckOfPlayingCardsQuantity, DeckOfPlayingCardsPPS, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText)
    end
end

我的问题是,为什么变量不变化?我试过放return price之类的,但还是没用.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-12 17:32:39

通过值传递变量price,而不是传递以参考。这个构造在Lua中不存在,因此您需要解决它,例如使用返回值:

代码语言:javascript
复制
DeckOfPlayingCardsPrice, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText = BuyItem(DeckOfPlayingCardsPrice, [...], DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText)

并正确返回预期值:

代码语言:javascript
复制
function BuyItem(price, quantity, pps, text, quantitytext)
    if(PixoosQuantity >= price) then
        [...]
    end
    return price, quantity, quantitytext
end

在Lua你可以返回多个结果

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35960681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档