我使用基本的java来生成它可以工作的随机整数,但问题是只要程序打开就会生成它。
目标是使用随机整数来决定每次创建对象时要渲染的图像。
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);
}
}
然后我使用这个类来创建块的对象数组
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;
}
}
}
答案 0 :(得分:0)
将精灵放入地图中。每个键都映射到枚举。它易于根据枚举或枚举的序数检索元素。
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())));
}
检索您的图片
private Random random = new Random();
private Sprite getRandomImage(){
Equipment[] values = Equipment.values();
return map.get(values[random.nextInt(values.length)]);
}