2017-04-17 15 views
-1

내 코드는 임의로 생성 된 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() { 
    } 

} 
+1

단지 관측 - 당신은 실제로 10 가지 랜덤 인스턴스가 필요하지 않습니다. 그냥 하나 만들고 다시 사용하십시오. –

+0

무작위로 개체를 쓰는 중 ... –

+0

모든'random.nextInt() ...'를 함수에 넣으십시오. 버튼을 눌렀을 때 그 기능을 호출하십시오. –

답변

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();

나는 또한 당신의 낙타 케이스가 일관성이 없다는 것을 알게됩니다. 일부 필드는 손상과 같은 대문자로 시작하고 다른 필드는 그렇지 않습니다. 가독성을 높이고 변수/필드가 소문자로 시작하는 것이 일반적입니다. 메소드 또한 일반적으로 get 및 generate와 같은 동사 여야합니다.

내가 할 수있는 한 최대한 질문에 대답하려면 단추를 클릭 할 때 호출되는 메서드를 찾아야합니다. 이 메소드에는 항목이 생성되고 렌더러에 새 항목에 대한 참조가 있는지 확인하십시오.

+0

이 (가) –

+0

에 일찍 게시 됨 Item 클래스를 사용하기 위해 렌더링 메소드에서 코드를 조정해야합니다. –

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(); 
     } 
}