Javafx 检测一个StackPane中的节点与另一个StackPane中的节点的冲突

Javafx 检测一个StackPane中的节点与另一个StackPane中的节点的冲突,javafx,intersection,Javafx,Intersection,作为前言,我对JavaFX编程非常陌生。(上学期我的一门课上介绍了JavaFX,上个月左右我一直在用JavaFX制作一个简单的游戏。) 我遇到的一个问题是试图检测一个StackPane中的窗格与另一个StackPane中的窗格的冲突。具体地说,我在游戏类中有一个“Player”节点(“Player”扩展了抽象的“Sprite”,它扩展了StackPane)以及一些“Asset”节点(“Asset”是一个抽象的父类,它像“Sprite”一样也扩展了StackPane)。“玩家”和每个“资产”节点都

作为前言,我对JavaFX编程非常陌生。(上学期我的一门课上介绍了JavaFX,上个月左右我一直在用JavaFX制作一个简单的游戏。)

我遇到的一个问题是试图检测一个StackPane中的窗格与另一个StackPane中的窗格的冲突。具体地说,我在游戏类中有一个“Player”节点(“Player”扩展了抽象的“Sprite”,它扩展了StackPane)以及一些“Asset”节点(“Asset”是一个抽象的父类,它像“Sprite”一样也扩展了StackPane)。“玩家”和每个“资产”节点都由ImageView和窗格对象组成,这些对象将成为节点的“边界”

下面是我如何在游戏类中尝试跟踪碰撞,但它不起作用:

protected void update() {
    // Call playerBoundsHandler in update cycle
    for (Asset asset : this.gameArea.getAssets()) {
        playerBoundsHandler(asset);
    } // for
} // update

private void playerBoundsHandler(Asset asset) {
    for (Pane boundary : asset.getAssetBoundaries()) {
        if (player.getPlayerStandingAreaBox()
        .getBoundsInParent()
        .intersects(boundary.getBoundsInParent())) {
            // do stuff here
        } // if
    } // for
} // playerBoundsHandler
我猜在这里使用getBoundsInParent()可能有问题,因为我试图跟踪两个单独节点中的子节点的交集,但我不知道解决方案是什么。是否需要使用getBoundsInLocal或其他方法执行某些操作

为了澄清起见,以下是玩家类的相关部分:

/** 
* Player class constructor. 
* Player class extends "Sprite" (abstract class)
* which extends StackPane.
*/
public Player(double xSpawn, double ySpawn) {
    // Add Player Standing Box (a Pane situated at the feet of the Player sprite)  
    this.playerStandingAreaBox = new Pane();
    // width, height, etc. set here
    this.getChildren().add(playerStandingAreaBox);
    this.setAlignment(playerStandingAreaBox, Pos.BOTTOM_CENTER);
} // Player constructor

public Pane getPlayerStandingAreaBox() {
    return this.playerStandingAreaBox;
} // getPlayerStandingAreaBox
资产子类遵循的设计与这里的Player类几乎相同。如果还需要澄清,以下是“公路”等级:

公共级公路延伸资产{
公路(双平移,双平移){
//这里叫super
setAssetBoundaries();
}//公路施工人员
@凌驾
setAssetBoundaries(){
窗格边界一=新窗格();
//设置boundaryOne设置
this.getChildren().add(boundaryOne);
this.assetBoundaries.add(boundaryOne);
窗格边界二=新窗格();
//设置边界两个设置
this.getChildren().add(boundaryTwo);
this.assetBoundaries.add(边界二);
}//setAssetBoundaries
/**

*assetBoundaries是一个数组列表

谢谢,James\u D。下面的更改正是我希望它做的

private void playerBoundsHandler(Asset asset) {
    for (Pane boundary : asset.getAssetBoundaries()) {
        Bounds boundaryBoundsInScene = boundary.localToScene(boundary.getBoundsInLocal());
        Bounds playerStandingBoxBoundsInScene = player.getPlayerStandingBoxArea()
            .localToScene(player.getPlayerStandingBoxArea().getBoundsInLocal());
        if (boundaryBoundsInScene.intersects(playerStandingBoundsInScene)) {
            // do stuff here
        } // if
    } // for
} // playerBoundsHandler

谢谢,James_D。下面的更改正是我希望它做的

private void playerBoundsHandler(Asset asset) {
    for (Pane boundary : asset.getAssetBoundaries()) {
        Bounds boundaryBoundsInScene = boundary.localToScene(boundary.getBoundsInLocal());
        Bounds playerStandingBoxBoundsInScene = player.getPlayerStandingBoxArea()
            .localToScene(player.getPlayerStandingBoxArea().getBoundsInLocal());
        if (boundaryBoundsInScene.intersects(playerStandingBoundsInScene)) {
            // do stuff here
        } // if
    } // for
} // playerBoundsHandler

我没有阅读您所有的问题,但您可能会在中找到相关信息:。我的猜测是,要获得更具针对性的答案,您可能需要以不同的方式提出问题,方法是提供(完整的、最少的代码,可以复制和粘贴以运行,但不能复制和粘贴整个应用程序,并且编写代码只是为了演示两个形状的碰撞问题,其他什么都没有)。如果它们有不同的父对象,比较父对象中的边界将不起作用(您正在为每个对象查看不同的坐标系)。您需要在它们共同的坐标系中比较它们的边界,例如场景。因此,例如
player.localToScene(player.getBoundsInLocal())
boundary.localToScene(boundary.getBoundsInLocal())
。谢谢你,James_D!自从我发布这个问题以来,我一直在跟踪这条线索,但还没有弄清楚如何准确地设置它。我已经让它完全按照我现在想做的去做,使用你提供的东西!@jewelsea对此表示抱歉。下次我在这里提问时,我会记住MRE。我写了一篇文章,可能会帮助你了解发生了什么ening。这是JavaFX2首次发布时编写的一个较旧的演示,因此它可能无法与最新的JavaFX版本一起开箱即用。我没有阅读您的所有问题,但您可以在中找到相关信息:。我的猜测是,要获得更具针对性的答案,您可能需要通过提供(完整的、最少的代码,可以复制和粘贴以运行,但不能复制和粘贴整个应用程序,并且编写代码只是为了演示两个形状的碰撞问题,其他什么都没有)。如果它们有不同的父对象,比较父对象中的边界将不起作用(您正在为每个对象查看不同的坐标系)。您需要在它们共同的坐标系中比较它们的边界,例如场景。因此,例如
player.localToScene(player.getBoundsInLocal())
boundary.localToScene(boundary.getBoundsInLocal())
。谢谢你,James_D!自从我发布这个问题以来,我一直在跟踪这条线索,但还没有弄清楚如何准确地设置它。我已经让它完全按照我现在想做的去做,使用你提供的东西!@jewelsea对此表示抱歉。下次我在这里提问时,我会记住MRE。我写了一篇文章,可能会帮助你了解发生了什么这是JavaFX2首次发布时编写的一个较旧的演示,因此它可能无法与最新的JavaFX版本一起开箱即用。