英文:
Adding a circle object to pane after button is clicked (JAVA FX)
问题
我正在制作一个游戏,用户可以控制一个人物对象。然而,我想在画布上添加一个圆形对象,以指示人物已经移动过的位置。简单地说,我想留下一个轨迹,显示用户移动过的地方。
我想知道如何在每次按方向按钮时向画布添加一个圆形对象。
这是我的代码:
// 上按钮的按钮动作事件
Up.setOnAction(new EventHandler
@Override
public void handle(ActionEvent e) {
// 向上移动对象
human.setLayoutY(human.getLayoutY() - 80);
// 保持对象在边界内
if (human.getLayoutY() < 40) {
human.setLayoutY(human.getLayoutY() + 80);
}
// 在当前位置添加一个圆形对象
Circle circle = new Circle(human.getLayoutX(), human.getLayoutY(), 5, Color.BLUE);
pane.getChildren().add(circle);
}
});
英文:
I am creating a game where the user can control the human object. However, I would like to add a circle object to the pane to indicate where the human has already travelled. In simpler terms, I would like to leave a trail indicating where the user has travelled.
I would like to know how to add a circle object to the pane every time I press a directional button.
Here is what I have:
//Button Action Event for up button
Up.setOnAction(new EventHandler <ActionEvent>() {
@Override
public void handle(ActionEvent e) {
//Move object Up
human.setLayoutY(human.getLayoutY()-80);
//Keep object within the boundaries
if(human.getLayoutY()<40) {
human.setLayoutY(human.getLayoutY()+80);
}
}
});
专注分享java语言的经验与见解,让所有开发者获益!
评论