从ArrayList<>转换随机对象

时间:2016-01-27 12:43:21

标签: java swing arraylist

您好,

我需要一些建议如何创建方法或更改将从我的ArrayList<>()中投射随机对象的Body代码;到SuperClass。我想让程序随机选择ArrayList中的敌人。 (这段代码有效,但为最后创建的敌人设置了标签。好吧,我希望自己清楚,对不起我的英语;)亲切的问候!

所以SuperClass基本上是"身体"我的程序与JFrame和标签:

NameEnemyTextLabel = new JLabel(Enemy.getName(Enemy.NameEnemy));
NameEnemyTextLabel.setBounds(150, 5, 100, 30);
add(NameEnemyTextLabel);

,子类是这样的:

class Enemy extends Body{

static int LevelEnemy;
static String NameEnemy;

static int StrengthEnemy;
static int AgilityEnemy;
static int StaminaEnemy;
static int InitiativeEnemy;

static int HealthEnemy;
static int DamageEnemy;

// Enemy stats - Strength
public Enemy() {
}

public Enemy (String NE, int LE, int SE, int AE, int StE, int IE) {

    NameEnemy = NE;
    LevelEnemy =LE;

    StrengthEnemy = SE;
    AgilityEnemy = AE;
    StaminaEnemy = StE;
    InitiativeEnemy = IE;

}

然后一些方法返回Name,Level等。然后,返回ArrayList:

//I was tryting to put ArrayList inside the main method and outside//

    public static ArrayList<Enemy> wrogowie = new ArrayList<Enemy>();

        Enemy enemy_01 = new Enemy ("Wróg 1",1,1,1,1,1);
        Enemy enemy_02 = new Enemy ("Wróg 2",2,2,2,2,2);
        Enemy enemy_03 = new Enemy ("Wróg 3",3,3,3,3,3);


public static void main(String[] args) {

    wrogowie.add(enemy_01);
    wrogowie.add(enemy_02);
    wrogowie.add(enemy_03);

}

}

0 个答案:

没有答案