如何在javafx中旋转后,使imageView沿着它面向的方向移动

时间:2017-04-18 07:16:46

标签: javafx

我正在使用javafx制作游戏,小鸡形象是我的主要玩家。我使用Imageview显示小鸡图像。我希望这只小鸡不断地飞奔,当我按下向上箭头时,我希望它停止旋转并朝着它面向的方向移动。 我用setRotate旋转我的小鸡。当我执行代码时,我的小鸡旋转得很好但是当我按下向上箭头键盘时,它会向随机方向移动。我该如何解决?

    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;
}


    }

1 个答案:

答案 0 :(得分:0)

在计算罪和cos之前,你必须将角度转换为弧度。 (Math.toRadians())