【问题标题】:Love2d: random element from tableLove2d:表中的随机元素
【发布时间】:2015-05-03 04:16:39
【问题描述】:

我正在尝试从表中随机获取一个项目。我在网上搜索过,但我找到的所有代码都不起作用。我的表是这样的:

section = {a, b}
love.graphics.newImage("/Images/a.png")
love.graphics.newImage("/Images/b.png")     
love.graphics.draw(section[imath.random(#section)], x, y) 

我需要这张桌子上的随机物品。

【问题讨论】:

标签: arrays lua lua-table love2d


【解决方案1】:

试试这个:

item = section[math.random(#section)]

在你的例子中:

section = {
   love.graphics.newImage("/Images/a.png"),
   love.graphics.newImage("/Images/b.png"),
}    
love.graphics.draw(section[math.random(#section)], x, y) 

【讨论】:

  • @math.random,如果你想要字母,请使用section = {"a", "b", "c", "f", "z"}
  • 我需要这个来加载一个随机的 .png。这是代码:love.graphics.newImage("/Images/a.png") .... love.graphics.newImage("/Images/b.png") love.graphics.draw(section[imath.random(#section)], x, y)
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 2020-05-10
  • 2021-12-30
  • 2014-06-12
  • 2015-09-22
  • 2021-01-31
  • 2012-02-07
  • 1970-01-01
相关资源
最近更新 更多