代码之家  ›  专栏  ›  技术社区  ›  user8340536

如何设置随机颜色

  •  3
  • user8340536  · 技术社区  · 7 年前

    这是我的代码摘录:

    Color[] color = new Color[3];
    color [0] = Color.red;
    color[1] = Color.blue;
    color[2] = Color.yellow;
    stage.getBatch().setColor(color[rand.nextInt()]);
    

    3 回复  |  直到 7 年前
        1
  •  5
  •   Ayush Khare    7 年前

    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
    
        2
  •  1
  •   JK Ly    7 年前

    Error message: "Cannot resolve method 'setColor (java.awt.Color)' 看起来您对Libgdx的SpriteBatch使用了错误的颜色类。

    com.badlogic.gdx.graphics.Color

    此外,要从3种颜色中正确随机选择,您需要使用 rand.nextInt(color.length) 将随机生成的int绑定到数组中的元素数。

        3
  •  0
  •   ucMedia    6 年前

    看看这个 Semi-random approach 这会使颜色更加闪亮和清新。

    enter image description here