我的代码打开一个窗口,其中显示了类似Rpg的项目的统计信息,这是随机生成的,现在我的问题是,我不知道如何制作它所以它按下后生成一个新项目按钮。我正在使用libgdx框架。 在编码时,我是一个血腥的新手,这几乎是我的第一个应用程序。 这是我的代码:
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() {
}
}
答案 0 :(得分:1)
正如你自己所说,你是一个新秀。所以一些提示是有序的。确保你带相关的变量并将它们分成如下类:
public class Item
{
int prefixIndex;
int nameIndex;
int typeIndex;
int itemLevel;
int damage;
int weaponSpeed;
int weaponStrengh;
int weaponEndurance;
int weaponCritical;
int weaponWisdom;
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"};
public Item(int prefixIndex, int nameIndex, int typeIndex)
{
this.prefixIndex = prefixIndex;
this.nameIndex = nameIndex;
this.typeIndex = typeIndex;
}
public int getPrefixIndex()
{
return prefixIndex;
}
public void setPrefixIndex(int prefixIndex)
{
this.prefixIndex = prefixIndex;
}
public int getNameIndex()
{
return nameIndex;
}
public void setNameIndex(int nameIndex)
{
this.nameIndex = nameIndex;
}
public int getTypeIndex()
{
return typeIndex;
}
public void setTypeIndex(int typeIndex)
{
this.typeIndex = typeIndex;
}
public int getItemLevel()
{
return itemLevel;
}
public void setItemLevel(int itemLevel)
{
this.itemLevel = itemLevel;
}
public int getDamage()
{
return damage;
}
public void setDamage(int damage)
{
this.damage = damage;
}
public int getWeaponSpeed()
{
return weaponSpeed;
}
public void setWeaponSpeed(int weaponSpeed)
{
this.weaponSpeed = weaponSpeed;
}
public int getWeaponStrengh()
{
return weaponStrengh;
}
public void setWeaponStrengh(int weaponStrengh)
{
this.weaponStrengh = weaponStrengh;
}
public int getWeaponEndurance()
{
return weaponEndurance;
}
public void setWeaponEndurance(int weaponEndurance)
{
this.weaponEndurance = weaponEndurance;
}
public int getWeaponCritical()
{
return weaponCritical;
}
public void setWeaponCritical(int weaponCritical)
{
this.weaponCritical = weaponCritical;
}
public int getWeaponWisdom()
{
return weaponWisdom;
}
public void setWeaponWisdom(int weaponWisdom)
{
this.weaponWisdom = weaponWisdom;
}
}
我个人会在这个类中加入一些变量并将它们进一步分开,以便类不那么大。但这可能会让你在你的水平上迷惑。但是使用这个类我可以像这样重写你的代码:
哦,我也不知道libgdx是如何工作的,所以你需要了解你的框架,知道实际放置这段代码的位置:
public Item generateItem()
{
Random random = new Random(System.nanoTime());
int prefix = random.nextInt(Item.Prefix.length);
int name = random.nextInt(Item.Name.length);
int type = random.nextInt(Item.ItemType.length);
temp.setItemLevel(random.nextInt(1000));
temp.setDamage(random.nextInt(25000));
temp.setWeaponSpeed(random.nextInt(100));
temp.setWeaponStrengh(random.nextInt(350));
temp.setWeaponEndurance(random.nextInt(350));
temp.setWeaponWisdom(random.nextInt(350));
temp.setWeaponCritical(random.nextInt(350));
}
然后有一个实例变量Item generatedItem
,只要你需要一个新变量,只需拨打generatedItem = generateItem();
我也注意到你的骆驼外壳不一致。有些领域的开头是像Damage这样的大写字母,有些则没有。它有助于提高可读性,一般的良好做法是变量/字段以小写字母开头。方法也一样,它们通常应该是get和generate之类的动词。
要尽我所能回答您的问题,您需要找到按下按钮时调用的方法。在该方法中生成一个项目,并确保渲染器具有对新项目的引用。
答案 1 :(得分:0)
只要您想通过随机数更改值,请致电generateRandom()
。
public class XYZ extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
Color rarityColor;
String rarity;
Random random;
String [] Prefix,Name,ItemType ;
int index,index1,index2,ItemLevel, Damage ,WeaponSpeed,WeaponStrengh ,WeaponEndurance,weaponCritical,weaponWisdom;
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
random=new Random();
Prefix = new String[]{"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"};
Name = new String[] {" 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"};
ItemType = new String[]{"Axe", "Sword", "Broadsword", "Dagger", "Bow","Longbow","Staff","Spear","Hammer"};
generateRandom();
}
@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();
}
public void generateRandom(){
index = random.nextInt(Prefix.length);
index1 = random.nextInt(Name.length);
index2 = random.nextInt(ItemType.length);
ItemLevel = random.nextInt(1000);
Damage = random.nextInt(25000);
WeaponSpeed = random.nextInt(100);
WeaponStrengh = random.nextInt(350);
WeaponEndurance = random.nextInt(350);
weaponCritical = random.nextInt(350);
weaponWisdom = random.nextInt(350);
}
@Override
public void dispose() {
font.dispose();
batch.dispose();
}
}