首页 > 解决方案 > 如何在 javafx 中为 2d 游戏对象创建传感器

问题描述

在我的作业中,我必须用 javafx 编写一个 2d parcour-game。如何向我的玩家(游戏对象)添加传感器,以便它可以打印出到下一个障碍的距离?

为了创建我使用圆圈的障碍/墙,我的玩家也是一个圆圈。谁能帮我解决这个问题?

我解释说,当我控制玩家时,传感器会检测到他面前最近的物体,并打印出物体的距离和位置(x/y)。

这是我构建墙壁和播放器的主要/控制类。我希望这有助于理解我的问题。这就是程序现在的样子。我也很高兴得到一些编码建议。

public class ParkourApp extends Application {

private List<Point2D> points = new ArrayList<>();
private List<Circle> circles = new ArrayList<>();
private Player p1;


public Parent createContent (){
    BorderPane root = new BorderPane();
    Pane draw = new Pane();
    Circle c1 = new Circle(100,0,5);
    Circle c2 = new Circle(200,0,5);
    Circle c3 = new Circle(300,0,5);
    Circle c4 = new Circle(400,0,5);

    Circle c5 = new Circle(0,100,5);
    Circle c6 = new Circle(0,200,5);
    Circle c7 = new Circle(0,300,5);
    Circle c8 = new Circle(0,400,5);


    addVerticalStraight(100,100,100,50,6);
    addHorizontalStraight(150,50,100,50,6);
    addVerticalStraight(140,250,50,50,6);
    addVerticalStraight(300,150,100,50,6);
    addStraight(100,90,140,50,9);
    addStraight(100,210,135,250,8);
    addStraight(158,205,190,240,8);
    addStraight(260,50,330,80,9);
    addStraight(340,85,350,140,8);
    addStraight(258,105,300,140,8);
    addAllCircles();

    AnimationTimer at = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };
    at.start();

    for(Circle c : circles){
        draw.getChildren().add(c);
    }

    p1 = new Player(new Rectangle(50,400,11,5));
    p1.setGeschwindigkeit(new Point2D(1,0));
    System.out.printf("xx:%f YY:%f\n",p1.getNode().getTranslateX(),p1.getNode().getTranslateY());

    draw.getChildren().addAll(c1,c2,c3,c4,c5,c6,c7,c8,p1.getNode());

    root.setCenter(draw);
    return root;
}

private void update(){
    for(Circle c : circles){
        if(p1.isColliding(c)){
            System.out.println("is Colliding..");
        }
    }
    p1.update();
}

private void addAllCircles(){
    for(Point2D po : points){
        Circle c = new Circle(po.getX(),po.getY(),3, Color.DIMGREY);
        circles.add(c);
    }
}

private ArrayList<Point2D> addStraight(double startX, double startY, double endX, double endY, double divisor){
    ArrayList<Point2D> list = new ArrayList<>();
    double length = 0;
    double xDist = 0;
    double m = 0;
    if(startX != endX) {
        if(endX < startX){
            double temp = startX;
            startX = endX;
            endX = temp;
            temp = startY;
            startY = endY;
            endY = temp;
        }
        //upgrade of the straight
        m = -(startY-endY)/(endX - startX);
        System.out.println(m);

        //Distance of the two points
        length = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
        System.out.println(length);

        //xDist is the distance of every point of the straight on the x-axis
        xDist = endX - startX;
        xDist /= divisor;
        System.out.println(xDist);


        double x = startX;
        double y = startY;
        //this loop uses all the data that was calculated and creates a   
        //2D-Point which is stored in class-list of the circles
        for(int i=0; i<divisor+1; i++){
            list.add(new Point2D(x, y));
            points.add(new Point2D(x, y));
            y += (xDist * m);
            System.out.println(y);
            x += xDist;
        }
    }
    return list;
}

private ArrayList<Point2D> addHorizontalStraight(double x, double y, double length, double width, double dist){
    int amount = (int)length/(int)dist;
    ArrayList<Point2D> list = new ArrayList<>();
    for(int i=0; i<amount+1; i++){
        list.add(new Point2D(x,y));
        list.add(new Point2D(x,y+width));
        points.add(new Point2D(x,y));
        points.add(new Point2D(x,y+width));
        x += dist;
    }
    return list;
}

private ArrayList<Point2D> addVerticalStraight(double x, double y, double length, double width, double dist){
    int amount = (int)length/(int)dist;
    ArrayList<Point2D> list = new ArrayList<>();
    for(int i=0; i<amount+1; i++){
        list.add(new Point2D(x,y));
        list.add(new Point2D(x+width,y));
        points.add(new Point2D(x,y));
        points.add(new Point2D(x+width,y));
        y += dist;
    }
    return list;
}

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setScene(new Scene(createContent(),600,600));
    primaryStage.getScene().setOnKeyReleased(e ->{
        switch (e.getCode()){
            case LEFT:
                p1.turnLeft();
                break;
            case RIGHT:
                p1.turnRight();
                break;
            case UP:
                p1.faster();
                break;
            case DOWN:
                p1.slower();
                break;
        }
    });


    primaryStage.show();
}

}

标签: javajavafx

解决方案


推荐阅读