在功能中改变变量

时间:2016-03-12 17:06:11

标签: lua corona

当我按下一个按钮时,我希望这样做,一堆变量会改变。

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 个答案:

答案 0 :(得分:1)

您按值传递变量DeckOfPlayingCardsPrice, DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText = BuyItem(DeckOfPlayingCardsPrice, [...], DeckOfPlayingCardsText, DeckOfPlayingCardsQuantityText) 而不是by reference。 Lua中不存在此构造,因此您需要解决它,例如使用返回值:

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

并正确返回预期值:

g3.rotate(Math.toRadians(Double.valueOf(getJtfRotationAngle().getText()))
        ,eye.getEyePos().getX_pos()
        ,eye.getEyePos().getY_pos());

g3.setColor(Color.BLACK);

g3.draw(new Line2D.Double(eye.getEyePos().getX_pos(),eye.getEyePos().getY_pos()
        ,eye.getEyePos().getX_pos()+25,eye.getEyePos().getY_pos()));
g3.draw(new Line2D.Double(eye.getEyePos().getX_pos(),eye.getEyePos().getY_pos()
        ,eye.getEyePos().getX_pos(),eye.getEyePos().getY_pos()+25));

//This line is the orientation vector
g3.draw(new Line2D.Double(eye.getEyePos().getX_pos()
        ,eye.getEyePos().getY_pos()
        ,eye.getEyePos().getX_pos()+25
        ,eye.getEyePos().getY_pos()+25));

g3.drawOval((int)eye.getEyePos().getX_pos(),(int)eye.getEyePos().getY_pos(),10,10);
g3.fillOval((int)eye.getEyePos().getX_pos(),(int)eye.getEyePos().getY_pos(),10,10);

eye.setEyeTruePos(new Point(eye.getEyePos().getX_pos()-x_pos/2,eye.getEyePos().getY_pos()-y_pos/2));

在Lua,你可以return multiple results