Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import javafx.animation.*; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.animation.KeyFrame; import javafx.util.Duration; import javafx.scene.image.ImageView; import
import javafx.animation.*; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.animation.KeyFrame; import javafx.util.Duration; import javafx.scene.image.ImageView; import java.util.ArrayList; import java.util.List; public class Animation extends Application { Image c1 = new Image("File:src/animation/Run (1).png"); Image c2 = new Image("File:src/animation/Run (2).png"); Image c3 = new Image("File:src/animation/Run (3).png"); Image c4 = new Image("File:src/animation/Run (4).png"); Image c5 = new Image("File:src/animation/Run (5).png"); Image c6 = new Image("File:src/animation/Run (6).png"); Image c7 = new Image("File:src/animation/Run (7).png"); Image c8 = new Image("File:src/animation/Run (8).png"); Image tree= new Image("File:src/animation/Tree.png"); Image skeleton = new Image("File:src/animation/Skeleton.png"); Image bg = new Image("File:src/animation/BG.png"); List list = new ArrayList(); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { list.add(new ImageView(c1)); list.add(new ImageView(c2)); list.add(new ImageView(c3)); list.add(new ImageView(c4)); list.add(new ImageView(c5)); list.add(new ImageView(c6)); list.add(new ImageView(c7)); list.add(new ImageView(c8)); for(int i = 0; imillis(100), t -> cat.getChildren().setAll(list.get(0))), new KeyFrame(Duration.millis(200), t -> cat.getChildren().setAll(list.get(1))), new KeyFrame(Duration.millis(300), t -> cat.getChildren().setAll(list.get(2))), new KeyFrame(Duration.millis(400), t -> cat.getChildren().setAll(list.get(3))), new KeyFrame(Duration.millis(500), t -> cat.getChildren().setAll(list.get(4))), new KeyFrame(Duration.millis(600), t -> cat.getChildren().setAll(list.get(5))), new KeyFrame(Duration.millis(700), t -> cat.getChildren().setAll(list.get(6))), new KeyFrame(Duration.millis(800), t -> cat.getChildren().setAll(list.get(7))) ); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); //Then I created a path and transition for the walking cat to follow //How can I, instead of Indefinite, just when end of path - // flip the sprite around and let it walk new path back and then loop that all indefinite PathTransition pathTransition=new PathTransition(); Path path =new Path(); path.getElements().add(new MoveTo(50,600)); path.getElements().add(new LineTo(50,600)); path.getElements().add(new LineTo(830,600)); path.setVisible(false); pathTransition.setDuration(Duration.seconds(5)); pathTransition.setPath(path); pathTransition.setNode(cat); pathTransition.setCycleCount(Timeline.INDEFINITE); pathTransition.setAutoReverse(true); pathTransition.play(); Group root = new Group(); root.getChildren().addAll(iBG,treeIm, skel, cat, path); Scene scene = new Scene(root, 1000, 800); primaryStage.setTitle("Graveyard Kitten"); primaryStage.setScene(scene); primaryStage.show(); } }
I am letting a sprite walk across my screen(background), I managed to combine the movements into a nice rhythm (inside a group), then I moved it from a to b by path transition - still all good. But how can I flip it, so when it walks back from b to a, its face should turn in walking direction? I did add this code , but the I had to quit using INDEFINITE..
pathTransition.setOnFinished(new EventHandler() { @Override public void handle(ActionEvent event) { for(int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started