类javafx.scene.layout.anchorpane不能强制转换为类sm\u cw.model.intersect

7gcisfzg  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(170)

我正在尝试为我的游戏加载一个fxml,当点击开始按钮时它会通过其他类,但是当它到达这个类(actor.java)时,这个错误会弹出,我理解这个错误的含义,但是我不知道如何在这个上下文中解决它。
类(包含描述游戏中对象的方法)

public abstract class Actor extends ImageView{

    private double xpos;
    private double ypos;

    public Actor(String imageLink, double xpos, double ypos) {
        setImage(new Image(imageLink));
        this.xpos = xpos;
        this.ypos = ypos;
    }

    public void resetActor() {
        setX(xpos);
        setY(ypos);
    }

    /**
     * to set the movement speed of obstacles & frog
     * @param dx
     * @param dy
     */
    public void move(double dx, double dy) {
        setX(getX() + dx);
        setY(getY() + dy);
    }

    public Intersect getIntersect(){
        return (Intersect) getParent();
    }

    public <A extends Actor> java.util.List<A> getIntersectingObjects(java.lang.Class<A> cls){
        ArrayList<A> someArray = new ArrayList<A>();
        for (A actor: getIntersect().getObjects(cls)) {
            if (actor != this && actor.intersects(this.getBoundsInLocal())) {
                someArray.add(actor);
            }
        }
        return someArray;
    }

intersect.java(当动物(主要游戏对象)与障碍物相交时的方法)

public class Intersect extends Pane {

    public Intersect() {

        sceneProperty().addListener(new ChangeListener<Scene>() {

            @Override
            public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene newValue) {
                if (newValue != null) {
                    newValue.setOnKeyReleased(new EventHandler<KeyEvent>() {

                        @Override
                        public void handle(KeyEvent event) {
                            if (getOnKeyReleased() != null)
                                getOnKeyReleased().handle(event);
                            List<Actor> myActors = getObjects(Actor.class);
                            for (Actor anActor : myActors) {
                                if (anActor.getOnKeyReleased() != null) {
                                    anActor.getOnKeyReleased().handle(event);
                                }
                            }
                        }

                    });

                    newValue.setOnKeyPressed(new EventHandler<KeyEvent>() {

                        @Override
                        public void handle(KeyEvent event) {
                            if (getOnKeyPressed() != null)
                                getOnKeyPressed().handle(event);
                            List<Actor> myActors = getObjects(Actor.class);
                            for (Actor anActor : myActors) {
                                if (anActor.getOnKeyPressed() != null) {
                                    anActor.getOnKeyPressed().handle(event);
                                }
                            }
                        }

                    });
                }

            }

        });

    }
    public <A extends Actor> List<A> getObjects(Class<A> cls) {
        ArrayList<A> someArray = new ArrayList<A>();
        for (Node n: getChildren()) {
            if (cls.isInstance(n)) {
                someArray.add((A)n);
            }
        }
        return someArray;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题