0
我使用javafx製作遊戲,小雞圖片是我的主要玩家。我使用Imageview顯示小雞圖像。我希望這個小妞不斷地發光,當我按下向上箭頭時,我希望它停止旋轉並朝着它所面對的方向移動。 我使用setRotate來旋轉我的小雞。當我執行代碼時,我的小雞旋轉得很好,但是當我按下向上箭頭鍵盤時,它朝着隨機方向移動。我該如何解決它?如何讓imageView在javafx中旋轉後面向它的方向移動
package javagame;
import java.util.ArrayList;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.NodeOrientation;
import javafx.geometry.Point3D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class JavaGame extends Application {
@Override
public void start(Stage primaryStage) {
Chick test = new Chick(1);
test.setPosition(300,200);
StackPane root = new StackPane();
root.getChildren().addAll(test);
Scene scene = new Scene(root, 900, 700);
ArrayList<String> input = new ArrayList<String>();
scene.setOnKeyPressed(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
if (!input.contains(code))
input.add(code);
}
});
scene.setOnKeyReleased(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
input.remove(code);
}
});
new AnimationTimer(){
public void handle(long now){
System.out.println(test.getNodeOrientation());
System.out.println("effective " +test.getEffectiveNodeOrientation());
if(input.contains("UP")){
test.move();
System.out.println(test.getAngle());
System.out.println(test.getPosition());
System.out.println("getRotate " +test.getRotate());
System.out.println("local" +test.getLocalToSceneTransform());
System.out.println("parent" +test.getLocalToParentTransform());
}
else{
test.stop();
System.out.println(test.getAngle());
System.out.println(test.getPosition());
System.out.println("local" +test.getLocalToSceneTransform());
System.out.println("parent" +test.getLocalToParentTransform());
}
}
}.start();
primaryStage.setTitle("java game");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
這裏就是我對球員
package javagame;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
public class Chick extends Pane{
private Image [] img = {new Image("file:img/0.png"),
new Image("file:img/pcTop.png"), new Image("file:img/rcTop.png"),
new Image("file:img/ycTop.png"),new Image("file:img/gcTop.png")};
private ImageView imageView;
private double x;
private double y;
private double vx=1;
private double vy=1;
private double angle=0;
private double q;
private double toAngle = 1;
private Timeline rotateAnimation;
private Timeline translateAnimation;
public Chick(){
}
public Chick(int num){
imageView = new ImageView(img[num]);
//vx=5; vy=5;
getChildren().clear();
getChildren().addAll(imageView);
rotateAnimation = new Timeline(
new KeyFrame(Duration.millis(100), e ->{ spin();} ));
rotateAnimation.setCycleCount(Timeline.INDEFINITE);
rotateAnimation.play();
translateAnimation = new Timeline(
new KeyFrame(Duration.millis(20), e ->{ move();} ));
translateAnimation.setCycleCount(Timeline.INDEFINITE);
}
public void setPosition(double x, double y){
this.x = x;
this.y = y;
imageView.setTranslateX(x);
imageView.setTranslateY(y);
}
public String getPosition(){
return "(" +x+ ", " +y+ ")";
}
public double getQuardant(){
if(angle>0 && angle<90)q=1;
if(angle>90 && angle<180)q=2;
if(angle>180 && angle<270)q=3;
if(angle>270 && angle<360)q=4;
else q=0;
return q;
}
public void move(){
translateAnimation.play();
rotateAnimation.stop();
x += vx*cos(angle);
y += vy*sin(360-angle);
setPosition(x,y);
//this.relocation(x,y);
}
public void stop(){
rotateAnimation.play();
translateAnimation.stop();
}
public void spin(){
if(angle < 359)angle += toAngle;
else angle=0;
imageView.setRotate(angle);
}
public double getAngle(){
return angle;
}
public void setToAngle(double d){
toAngle = d;
}
}
雖然我已經修好了,謝謝你的回答:)) –