Mein Code öffnet ein Fenster, wo die Statistiken eines RPG-ähnlichen Items angezeigt werden, die nach dem Zufallsprinzip erzeugt wurden, jetzt ist mein Problem, dass ich nicht weiß, wie ich es machen soll, damit es ein neues Item erzeugt nach dem Drücken einer Taste. Ich benutze das libgdx-Framework. Wenn es um das Codieren geht, bin ich ein blutiger Anfänger, und das ist so ziemlich meine erste Anwendung. Hier ist mein Code:Generiere neue zufällige Werte
package com.mygdx.test;
import java.util.Random;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Itemgenerator extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
Color rarityColor;
String rarity;
Random randomPrefix = new Random();
Random randomName = new Random();
Random randomRarity = new Random();
Random randomItemType = new Random();
Random randomItemLevel = new Random();
Random randomDamage = new Random();
Random randomWeaponSpeed = new Random();
Random randomStrengh = new Random();
Random randomEndurance = new Random();
Random randomCritical = new Random();
Random randomWisdom = new Random();
String [] Prefix = {"Blunt", "Sharp", "Jagged", "Grandmaster's", "Knight's", "Apprentice's","Crude", "Godly", "Flawless", "Barbaric", "Horrific", "Hiddeous", "Demonic", "Dull", "Bloody", "Holy", "Engergiced", "Fast", "Otherworldly", "Well-Worn", "Elegant","Vigilant", "Surpressing" ,"Destroying", "Vampiric", "Intimidating"};
String [] Name = {" Soulsplitter"," Axe", " Sword", " Spear", " Bow", " Longbow", " Staff", " God Eater", " Doomsday Device", " Excalibur", " Nature's Call", " Forest Spirit", " Dragon's Breath", " God's Wrath", " Buster", " Peace Keeper", " Jackhammer", " Battleaxe", " Emperor's Lance", " Tsunami", " Hurricane"};
String [] ItemType = {"Axe", "Sword", "Broadsword", "Dagger", "Bow","Longbow","Staff","Spear","Hammer"};
int index = randomPrefix.nextInt(Prefix.length);
int index1 = randomName.nextInt(Name.length);
int index2 = randomItemType.nextInt(ItemType.length);
int ItemLevel = randomItemLevel.nextInt(1000);
int Damage = randomDamage.nextInt(25000);
int WeaponSpeed = randomWeaponSpeed.nextInt(100);
int WeaponStrengh = randomStrengh.nextInt(350);
int WeaponEndurance = randomEndurance.nextInt(350);
int weaponCritical = randomCritical.nextInt(350);
int weaponWisdom = randomWisdom.nextInt(350);
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch,Prefix[index]+ Name[index1], 0, 300);
if(ItemLevel<=101){
rarity = "Common";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=251){
rarity = "Uncommon";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=401){
rarity = "Rare";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=551){
rarity = "Magical";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=626){
rarity = "Epic";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=736){
rarity = "Unique";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=876){
rarity = "Legendary";
font.draw(batch, rarity, 0, 280);
}
else if(ItemLevel<=1000){
rarity = "Glitched";
font.draw(batch, rarity, 0, 280);
}
if (rarity == "Common"){
font.setColor(Color.WHITE);
}
else if(rarity == "Uncommon"){
font.setColor(Color.LIME);
}
else if(rarity == "Rare"){
font.setColor(Color.BLUE);
}
else if(rarity == "Magical"){
font.setColor(Color.PURPLE);
}
else if(rarity == "Epic"){
font.setColor(Color.PINK);
}
else if(rarity == "Unique"){
font.setColor(Color.YELLOW);
}
else if(rarity == "Legendary"){
font.setColor(Color.ORANGE);
}
else if(rarity == "Glitched"){
font.setColor(Color.RED);
}
font.draw(batch, "ItemLevel: "+ItemLevel, 140, 280);
font.draw(batch, ItemType[index2], 0, 260);
font.draw(batch, "Damage: "+Damage, 0, 220);
font.draw(batch, "Weaponspeed: "+WeaponSpeed, 140, 220);
font.draw(batch, "+"+WeaponStrengh+" Strengh", 0, 200);
font.draw(batch, "+"+WeaponEndurance+" Endurance", 0, 180);
font.draw(batch, "+"+weaponCritical+" Critical Strike", 0, 160);
font.draw(batch, "+"+weaponWisdom+" Wisdom", 0, 140);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
Nur eine Beobachtung - Sie brauchen wirklich nicht 10 verschiedene zufällige Instanzen. Einfach eins machen und es wiederverwenden. –
Zufällige Objekte für Tage ... –
Setzen Sie all Ihre 'random.nextInt() ...' in eine Funktion. Rufen Sie diese Funktion auf, wenn eine Schaltfläche gedrückt wird –