我的问题是,我是否以某种方式在我的程序中实现了多线程,因为在我的程序中,在包含2个图像的Controller类中,当单击图像时,控件应该运行到paraopen或paraopen2(取决于哪个图像单击)但真正发生的是paraopen和paraopen2方法一个接一个地点击两个图像后同时执行。我认为应该发生的事情是,如果打开paraopen(通过点击其各自的图像),控制应该进入paraopen并且在完成paraopen后应该初始化然后到paraopen2(通过点击其repsective图像)。方法(初始化,paraopen和paraopen2)运行就好像在三个不同的线程中运行,这是我最初想要的(确定有很多重复的代码,但它有效)。
有人可以帮助我理解paraopen和paraopen2这两个函数是如何同时工作的。播放器类是我的主类,控制器类包含我想要理解的代码。
先谢谢。
这是我的玩家类(主要类)
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
public class Controller implements Initializable {
@FXML
ImageView player1;
@FXML
ImageView playero1;
@FXML
ImageView player2;
@FXML
ImageView playero2;
TranslateTransition transition = new TranslateTransition();
TranslateTransition transition1 = new TranslateTransition();
TranslateTransition transition2 = new TranslateTransition();
TranslateTransition transition2_ = new TranslateTransition();
TranslateTransition transition3 = new TranslateTransition();
TranslateTransition transition3_ = new TranslateTransition();
TranslateTransition transition4 = new TranslateTransition();
TranslateTransition transition4_ = new TranslateTransition();
private double startDragX;
double x = 20;
double x1 = 120;
double y,y1;
int flag1 = 0;
int flag2 = 0;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
playero1.setVisible(false);
playero2.setVisible(false);
player1.setManaged(false);
player1.setTranslateX(x);
player1.setOnMousePressed(e -> {
playero1.setTranslateX(player1.getTranslateX());
y = player1.getTranslateY();
playero1.setTranslateY(player1.getTranslateY());
player1.setVisible(false);
transition1.pause();
playero1.setVisible(true);
paraopen();
});
//This code makes the player move downwards continuously
transition1.setDuration(Duration.seconds(15));
transition1.setNode(player1);
transition1.setToY(800);
transition1.play();
player2.setManaged(false);
player2.setTranslateX(120);
player2.setOnMousePressed(e -> {
playero2.setTranslateX(player2.getTranslateX());
y1 = player2.getTranslateY();
playero2.setTranslateY(player2.getTranslateY());
player2.setVisible(false);
transition2.pause();
playero2.setVisible(true);
paraopen2();
});
//This code makes the player move downwards continuously
transition2.setDuration(Duration.seconds(15));
transition2.setNode(player2);
transition2.setToY(800);
transition2.play();
}
public void paraopen() {
playero1.setManaged(false);
playero1.setOnMousePressed(e -> {
startDragX = e.getSceneX();
flag1 = 1;
});
playero1.setOnMouseDragged(e -> {
if(flag1 ==1) {
double X = e.getSceneX();
if(X - startDragX > 0) {
x = x + 10;
playero1.setTranslateX(x);
flag1 = 0;
}
else if (X - startDragX < 0) {
x = x - 10;
playero1.setTranslateX(x);
flag1 = 0;
}
}
});
//This code makes the player move downwards continuously
transition.setDuration(Duration.seconds(19));
transition.setNode(playero1);
transition.setToY(800);
transition.play();
}
public void paraopen2() {
playero2.setManaged(false);
playero2.setOnMousePressed(e -> {
startDragX = e.getSceneX();
flag2 = 1;
});
playero2.setOnMouseDragged(e -> {
if(flag2 ==1) {
double X = e.getSceneX();
if(X - startDragX > 0) {
x1 = x1 + 10;
playero2.setTranslateX(x1);
flag2 = 0;
}
else if (X - startDragX < 0) {
x1 = x1 - 10;
playero2.setTranslateX(x1);
flag2 = 0;
}
}
});
//This code makes the player move downwards continuously
double r = ((800-y1)*19)/(800-y);
transition2_.setDuration(Duration.seconds(r));
transition2_.setNode(playero2);
transition2_.setToY(800);
transition2_.play();
}
}
这是我的新控制器类
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<ImageView fx:id="player1" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero1" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
<ImageView fx:id="player2" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefall.png" />
</image>
</ImageView>
<ImageView fx:id="playero2" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@freefallp.png" />
</image>
</ImageView>
</children>
</AnchorPane>
这是我的FXML文件
{{1}}