Java libgdx生成随机整数,但不断生成

Java libgdx生成随机整数,但不断生成,java,libgdx,Java,Libgdx,我使用basicjava生成它可以工作的随机整数,但问题是,只要程序打开,它生成的随机整数就会继续工作 目标是使用随机整数来决定每次创建对象时要渲染的图像 public void randomGen(){ randomInt = random.nextInt(100); System.out.println(randomInt); } 这只是我尝试的方法之一的一个例子 分组码 package com.brotiergames.ayabew; public class B

我使用basicjava生成它可以工作的随机整数,但问题是,只要程序打开,它生成的随机整数就会继续工作

目标是使用随机整数来决定每次创建对象时要渲染的图像

public void randomGen(){
    randomInt = random.nextInt(100);
    System.out.println(randomInt);

}
这只是我尝试的方法之一的一个例子

分组码

package com.brotiergames.ayabew;



public class Blocks  {
private TextureAtlas atlas;
private SpriteBatch batch;
private AtlasRegion sword,shield,staff,fireball,iceball;
private Random random;
private Sprite sprSword,sprShield,sprStaff,sprFireball,sprIceball;
public int randomInt;
public enum BLOCKTYPE{
    SWORD,SHIELD,FIREBALL,ICEBALL;
}
public Blocks(){
    this.atlas = new TextureAtlas(Gdx.files.internal("data/wizardpack.pack"));
    this.batch = new SpriteBatch();
    this.sword = atlas.findRegion("Sword");
    this.shield = atlas.findRegion("Shield");
    this.staff = atlas.findRegion("Staff");
    this.fireball = atlas.findRegion("flame");
    this.iceball = atlas.findRegion("icespell");
    this.sprSword = new Sprite(sword);
    this.sprShield = new Sprite(shield);
    this.sprStaff = new Sprite(staff);
    this.sprFireball = new Sprite(fireball);
    this.sprIceball = new Sprite(iceball);
    this.random = new Random();
    this.randomGen();

}
/*public void randomRenderofBlocks(float x,float y){
batch.begin();
    for ( int i = 0; i < 7; i++){
        //int tempInt = randomInt;
        //int thisInt = tempInt;
        //System.out.println(thisInt);
        if   (thisInt <= 25){
            sprShield.setBounds(x, y, 64, 64);
            sprShield.draw(batch);
        } else if (thisInt >=26 && thisInt <=50){
            sprSword.setBounds(x, y, 64, 64);
            sprSword.draw(batch);
        } else if (thisInt >=51 && thisInt <=75){
            sprFireball.setBounds(x, y, 64, 64);
            sprFireball.draw(batch);
        } else if (thisInt >= 76){
            sprIceball.setBounds(x, y, 64, 64);
            sprIceball.draw(batch);
        }
        x += 92;
        //y +=92;
    }
    batch.end();
}*/
public void renderBlockX(float x , float y){
    batch.begin();
    System.out.println("O"+randomInt);
    if   (randomInt <= 25){
        sprShield.setBounds(x, y, 64, 64);
        sprShield.draw(batch);
        BLOCKTYPE type = BLOCKTYPE.SWORD;
    } else if (randomInt >=26 && randomInt <=50){
        sprSword.setBounds(x, y, 64, 64);
        sprSword.draw(batch);
        BLOCKTYPE type = BLOCKTYPE.SHIELD;
    } else if (randomInt >=51 && randomInt <=75){
        sprFireball.setBounds(x, y, 64, 64);
        sprFireball.draw(batch);
        BLOCKTYPE type = BLOCKTYPE.FIREBALL;
    } else if (randomInt >= 76){
        sprIceball.setBounds(x, y, 64, 64);
        sprIceball.draw(batch);
        BLOCKTYPE type = BLOCKTYPE.ICEBALL;
    }
    x += 92;
    batch.end();
    System.out.println("N"+randomInt);
}
public void oldrenderBlocks(){
    //Gdx.gl20.glClearColor(0, 0, 0, 1);
    //Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    sprShield.setBounds(150, 170, 64, 64);
    sprShield.draw(batch);
    sprSword.setBounds(246, 150, 64, 64);
    sprSword.draw(batch);
    sprFireball.setBounds(342, 150, 64, 64);
    sprFireball.draw(batch);
    sprIceball.setBounds(438, 150, 64, 64);
    sprIceball.draw(batch);
    sprIceball.setBounds(530, 150, 64, 64);
    sprIceball.draw(batch);

    batch.end();
}
public void dispose(){
    atlas.dispose();
    batch.dispose();
}
public void randomGen(){
    randomInt = random.nextInt(100);
    System.out.println(randomInt);

}
package com.brotiergames.ayabew;
公营楼宇{
私人地图集;
专用SpriteBatch批次;
私人大西洋地区剑、盾、杖、火球、冰球;
私有随机;
私人雪碧剑,雪碧盾,雪碧棒,雪碧火球,雪碧球;
公共整数随机整数;
公共枚举块类型{
剑、盾、火球、冰球;
}
公共大厦(){
this.atlas=new TextureAtlas(Gdx.files.internal(“data/wizardpack.pack”);
this.batch=新的SpriteBatch();
this.Swarm=atlas.findRegion(“剑”);
this.shield=atlas.findRegion(“shield”);
this.staff=atlas.findRegion(“staff”);
this.fireball=atlas.findRegion(“火焰”);
this.iceball=atlas.findRegion(“冰咒语”);
这把剑=新精灵(剑);
this.sprShield=新精灵(盾);
this.sprStaff=新精灵(staff);
this.sprFireball=新精灵(fireball);
this.sprIceball=新雪碧(冰球);
this.random=新的random();
这个。randomGen();
}
/*公共空心块(浮动x、浮动y){
batch.begin();
对于(int i=0;i<7;i++){
//int-tempInt=randomInt;
//int-thisInt=tempInt;
//System.out.println(thisit);
如果(thisInt=26&&thisInt=51&&thisInt=76){
精神球后退(x,y,64,64);
sprIceball.draw(批);
}
x+=92;
//y+=92;
}
batch.end();
}*/
公共无效渲染块x(浮动x,浮动y){
batch.begin();
System.out.println(“O”+randomInt);
if(randomInt=26&&randomInt=51&&randomInt=76){
精神球后退(x,y,64,64);
sprIceball.draw(批);
BLOCKTYPE类型=BLOCKTYPE.IceCall;
}
x+=92;
batch.end();
System.out.println(“N”+randomInt);
}
公共无效oldrenderBlocks(){
//gl20.glClearColor(0,0,0,1);
//gl20.glClear(gl20.GL\u颜色\u缓冲\u位);
batch.begin();
1.立根(150、170、64、64);
绘制(批);
四.挫折(246、150、64、64);
1.抽签(批);
火球后退(34215064);
sprFireball.draw(批);
精神球.后坐(438150,64,64);
sprIceball.draw(批);
精神球.后退(530,150,64,64);
sprIceball.draw(批);
batch.end();
}
公共空间处置(){
atlas.dispose();
batch.dispose();
}
公共图书馆{
randomInt=random.nextInt(100);
System.out.println(randomInt);
}
}

然后我使用这个类为块创建对象数组

public class BlockArray  {
Blocks[] blockArray = new Blocks[6];
int offsetX = 0;
public void createArray(){
    for (int i = 0; i < blockArray.length; i++){
        blockArray[i] =  new Blocks();
        blockArray[i].renderBlockX(58 + offsetX, 170);
        offsetX +=92;

    }
}
公共类块数组{
Blocks[]blockArray=新块[6];
int offsetX=0;
公共void createArray(){
for(int i=0;i

}把你的精灵放到地图上。每个键都映射到一个枚举。它很容易根据元素的枚举顺序或枚举本身检索元素

private enum Equipment{
    Sword, Shield, Staff
}

Map<Equipment, Sprite> map = new EnumMap<Equipment,Sprite>(Equipment.class);
for(Equipment equipment : Equipment.values()){
    map.put(equipment, new Sprite(atlas.findRegion(equipment.name())));
}

您应该显示代码,在其中调用
randomGen()
;)从这个简短的代码片段中,您无法真正判断错误在哪里。我在构造函数中调用了它,并在拾取块的方法中调用了它。我添加了代码。现在,当我运行游戏时,精灵会出现和消失,通常它会从image1移动到Image4。谢谢,我需要知道它是哪种类型的块,以查看它的计数器。我假设剑是0等等?请看我更新的答案,您可以根据字符串、枚举或枚举序号检索图像。问题已经解决。我忘记添加map.get(),请确保map是一个字段,并且所有内容都可以正常工作。好的,我需要查看stracktrace。确保枚举与纹理名称匹配,即在我的示例中,我有纹理
private Random random = new Random();

private Sprite getRandomImage(){
    Equipment[] values = Equipment.values();
    return map.get(values[random.nextInt(values.length)]);
}