【问题标题】:How to pick up a random key from a table in Lua?如何从 Lua 中的表中获取随机密钥?
【发布时间】:2015-03-08 15:17:42
【问题描述】:

我正在使用此代码从该表中随机绘制图片

FishImages = {image1 = love.graphics.newImage("bg/fish1.png"),
            image2 = love.graphics.newImage("bg/fish2.png"),
            image3 = love.graphics.newImage("bg/fish3.png"),
            image4 = love.graphics.newImage("bg/fish4.png"),}

用这个函数love.graphics.draw({FishImages.image1#--I guess the modification is here },pos.x,pos.y)

那么如何从 Lua 中的表中获取随机键?

【问题讨论】:

    标签: random lua love2d


    【解决方案1】:

    math.random(1,4)14 范围内生成一个随机整数。所以你可以使用:

    FishImages['image' .. tostring(math.random(1,4))]
    

    【讨论】:

    • 谢谢,它可以工作,但不像我需要的那样,love2d 调用 love.graphics.draw 每个移动像素,这样我就可以同时移动 4 个图像
    猜你喜欢
    • 2021-04-19
    • 2017-10-29
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多