0
私はjavafxを使ってゲームを作っています。私はImageviewを使ってひよこ画像を表示します。私はこのひよこが絶えず欲しいと思っています。そして、私が上矢印を押したとき、それは回転を停止し、それが向いている方向に移動したいと思っています。 setRotateを使って私のひよこを回転させます。私がコードを実行すると、私のひよこはうまく回転しますが、上向き矢印キーを押すと、ランダムな方向に動きます。どうすれば修正できますか?javafxで回転させた後に直面する方向にimageViewを移動する方法
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);
}
}
そして、ここで、あなたの罪とあなたがラジアンに角度を変換する必要がCoS値を計算する前に、私は選手
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;
}
}
私はすでにそれを修正しましたが、あなたの答え:)) –