在按钮被点击后向窗格添加一个圆形对象 (JAVA FX)

huangapple 未分类评论51阅读模式
英文:

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 &lt;ActionEvent&gt;() {
			@Override
			public void handle(ActionEvent e) {
				//Move object Up
				human.setLayoutY(human.getLayoutY()-80);
				//Keep object within the boundaries
				if(human.getLayoutY()&lt;40) {
					human.setLayoutY(human.getLayoutY()+80);
				}
			}
		});

huangapple
  • 本文由 发表于 2020年5月5日 05:51:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61602239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定