我有三个三角形和三个正方形,每个三角形代表一个怪物。一次选择一个随机骰子掷骰,然后将选定的怪物移到屏幕中间(使用窗格,问题无法正常运行),然后进行三轮战斗,每轮掷骰显示在两个文本字段中在一个文本区域中,战斗中的动作是:“怪物1击中了怪物2,对其造成了2点伤害(伤害=掷骰子的差)。在三轮之后,怪物移回了位置,而我将moveBack()编码为实施,因为我被困在这里,问题是代码一直在运行,我不知道该怎么做才能使其停止并在继续进行之前进行第一场战斗。

public void playTheMonsterGame() {
    Timeline time = new Timeline();
    String[] hydNames = {"Heisenberg", "Jesse", "Todd"};
    String[] zexNames = {"Tuco", "Gustavo", "Lydia"};

    dice.setMonsters1and2();// sets which monster we want for switch
    switch (dice.monsterNum1) {
        case 1:
            trian.moveTri1();
            break;
        case 2:
            trian.moveTri2();
            break;
        case 3:
            trian.moveTri3();
            break;
        default:
            break;
    }

    switch (dice.monsterNum2) {
        case 1:
            square.moveSquare1();
            break;
        case 2:
            square.moveSquare2();
            break;
        case 3:
            square.moveSquare3();
            break;
        default:
            break;
    }

    dice.rollDice(); // random number generator generates all three rounds at once
    rollBox1 = ("Roll: " + dice.rolls[0][0] + ", " + dice.rolls[0][1] + ", " + dice.rolls[0][2] + ", " + dice.rolls[0][3]);
    rollBox2 = ("Roll: " + dice.rolls[3][0] + ", " + dice.rolls[3][1] + ", " + dice.rolls[3][2] + ", " + dice.rolls[3][3]);
    roll1.setText(rollBox1);
    roll2.setText(rollBox2); //roll is the text field
    int damage1;

    if (dice.total1a < dice.total4a) {
        damage1 = dice.total4a - dice.total1a;
    } else {
        damage1 = dice.total1a - dice.total4a;
    }
    System.out.println(damage1);
    trian.shrink(damage1);
    //jumboTron.setText(names[dice.monsterNum1]);
 }


我在一个do while循环中有它,但是显然不起作用

这也是移动的三角形类,所以您知道我正在使用什么:

public class Triangle {

public final Timeline timeline = new Timeline();

public void moveTri1() {
    timeline.setCycleCount(1);
    timeline.setAutoReverse(false);
    final KeyValue kvX = new KeyValue(tri1.layoutXProperty(), 274);
    final KeyValue kvY = new KeyValue(tri1.layoutYProperty(), 180);
    final KeyFrame kf = new KeyFrame(Duration.millis(1000), kvX, kvY);
    timeline.getKeyFrames().add(kf);
    timeline.play();
}

public void moveTri2() {
    timeline.setCycleCount(1);
    timeline.setAutoReverse(false);
    final KeyValue kvX = new KeyValue(tri2.layoutXProperty(), 274);
    final KeyFrame kf = new KeyFrame(Duration.millis(1000), kvX);
    timeline.getKeyFrames().add(kf);
    timeline.play();
}

public void moveTri3() {
    timeline.setCycleCount(1);
    timeline.setAutoReverse(false);
    final KeyValue kvX = new KeyValue(tri3.layoutXProperty(), 274);
    final KeyValue kvY = new KeyValue(tri3.layoutYProperty(), 180);
    final KeyFrame kf = new KeyFrame(Duration.millis(1000), kvX, kvY);
    timeline.getKeyFrames().add(kf);
    timeline.play();
}

public void moveBack() {
    timeline.setCycleCount(Timeline.INDEFINITE);
    timeline.setAutoReverse(false);
    final KeyValue kvX = new KeyValue(tri1.layoutXProperty(), 90);
    final KeyValue kvY = new KeyValue(tri1.layoutYProperty(), 60);
    final KeyFrame kf = new KeyFrame(Duration.millis(500), kvX, kvY);
    timeline.getKeyFrames().add(kf);
    timeline.play();
}

public void shrink(int damage) {
    double damPoints = damage/25.0;
    double damRescale = 25.0 - damPoints;
    System.out.println(damage+ " "+damPoints +" "+ damRescale);
    /*if(damRescale<=0)
        tri1.visibleProperty().setValue(false);
    else{
    tri1.setScaleX(damRescale);
    tri1.setScaleY(damRescale);}*/
 }
}

最佳答案

~~~>第1部分


每次调用moveTri1 moveTri2 moveTri3moveBack时,如果需要,则在TimeLine.Dunno上添加新的keyFrames,但如果不使用,则:

  timeline.getKeyFrames().clear();


在方法开始时清除时间轴中存在的先前关键帧。


  ~~~>关于moveBack方法


1)在moveBack方法的第一行中设置:

timeline.setCycleCount(Timeline.INDEFINITE);


所以时间轴会一次又一次地开始,但是当怪物想要向后移动时,它会执行一次而不是多次,因此可能需要:

timeline.setCycleCount(1);


2)您只向后移动了三角形1?如果向前移动的怪物是三角形2,则必须保存向前移动的怪物,然后再为给定的怪物调用moveBack()方法。

07-24 21:06