【问题标题】:Updating the location of a sprite on StackPane JavaFX在 StackPane JavaFX 上更新精灵的位置
【发布时间】:2013-10-26 11:09:00
【问题描述】:

我有一个关于 JavaFX 的问题。我自学了 Java,现在我正在学习 JavaFX。

我一直在尝试更新屏幕上 50x50 黑色块的位置。我有一个 YAxis 变量,当我更改时会更改块的位置。

我希望方块像俄罗斯方块一样在屏幕上“流动”。

我的代码很乱,因为我只是在搞乱它,所以请原谅:

package gamefx;

import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class GameFX extends Application {

  public Image img = new Image("Block1.png");
  public ImageView image = new ImageView(img);
  public int YAxis = -200;


  @Override
  public void start(Stage primaryStage) throws InterruptedException{
    primaryStage.setTitle("Game");
    StackPane stckp1 = new StackPane();
    Scene scn = new Scene(stckp1, 700, 700);

    primaryStage.setScene(scn);
    primaryStage.show();
    image.setTranslateY(YAxis);
    stckp1.getChildren().add(image);
  }
}

【问题讨论】:

    标签: java image javafx


    【解决方案1】:

    既然你想看到你的块移动,你需要一个动画。请参阅Oracle tutorial 了解更多信息。

    一个示例代码,快速编写:

    TranslateTransition tt = new TranslateTransition(Duration.millis(2000), image);
    tt.setByY(yAxis);
    tt.setCycleCount(1);
    tt.play();
    

    旁注: 变量名不得以大写字母开头。使用yAxis 而不是YAxis

    【讨论】:

    • 非常感谢,这帮助很大。我也把名字改成了 yAxis,谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多