Issue
I've an assignment to create a game like Fruit ninja. However, I faced a problem while trying to slice fruits because we are asked to just take action when the mouse hover over the fruits while we are pressing on it. In other words, i need function like "node.setOnMouseMoved(e ->{})" but only works while pressing on left mouse button.
Solution
Here is a starting point using the code from here. The only addition is the works while pressing on the left mouse button
part. To make this happen, I added an if-statement
that checks to see if the primary mouse button is down.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication368 extends Application
{
@Override
public void start(Stage primaryStage)
{
Circle cirle = new Circle(50);
StackPane root = new StackPane(cirle);
Scene scene = new Scene(root, 300, 250);
scene.addEventFilter(MouseEvent.DRAG_DETECTED, (MouseEvent mouseEvent) -> {
scene.startFullDrag();
});
cirle.setOnMouseDragEntered(mouseEvent -> {
if (mouseEvent.isPrimaryButtonDown()) {
System.out.println("slice!");
}
});
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Answered By - Sedrick