Slick2D - 基于坐标移动Sprite - Java

时间:2015-07-04 13:16:23

标签: java slick2d

我正在玩用于Java的Slick2D库的A* Path Finding Algorithm,并制作了一个带有精灵的简单地图,用于移动。
我还使用PathFinder算法来获取精灵必须遵循的一组坐标,以便从A点移动到B点。 现在,虽然我能够手动移动精灵",即使用键盘按下,我怎样才能根据我从PathFinder方法得到的这些点来设置它? ?

public void finder(TiledMap tm, int max_length, int start_x, int end_x, int start_y, int end_y) {
    try {
        TileBasedMap tbm = new PropertyBasedMap(tm, "blocked");
        AStarPathFinder pathFinder = new AStarPathFinder(tbm, max_length, false);

        Path path = pathFinder.findPath(null, start_x / 10, start_y / 10, end_x / 10, end_y / 10);
        int length = path.getLength();
        System.out.println("Found path of length: " + length + ".");

        for (int i = 0; i < length; i++) {
            curr_x = path.getX(i) * 10;
            curr_y = path.getY(i) * 10;

            System.out.println("Move From: " + (int) prev_x + "," + (int) prev_y + ".");
            System.out.println("Move To: " + (int) curr_x + "," + (int) curr_y + ".");
            System.out.println("------------------");

            //sprite.draw((int) curr_x, (int) curr_y);
            prev_x = curr_x;
            prev_y = curr_y;
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}  

很明显,sprite.draw()不起作用。此处spriteAnimation对象。

编辑1

我尝试更新update()功能,尝试模拟启用按键的功能。这就是我所拥有的:

    @Override
public void update(GameContainer container, int delta) {
    Input input = container.getInput();

    //On UserClick, get End Coordinates for PathFinding
    if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
        final_x = input.getMouseX();
        final_y = input.getMouseY();
        circle = new Circle(final_x, final_y, 10);

        TileBasedMap tbm = new PropertyBasedMap(grassMap, "blocked");
        AStarPathFinder pathFinder = new AStarPathFinder(tbm, max_length, false);
        Path path = pathFinder.findPath(null, (int) x / 10, (int) y / 10, final_x / 10, final_y / 10);
        int length = path.getLength();
        System.out.println("Found path of length: " + length + ".");

        for (int i = 0; i < length; i++) {
            curr_x = path.getX(i) * 10;
            curr_y = path.getY(i) * 10;

            System.out.println("Move From: " + (int) prev_x + "," + (int) prev_y + ".");
            System.out.println("Move To: " + (int) curr_x + "," + (int) curr_y + ".");

            if (curr_x - prev_x > 0) { //RIGHT
                System.out.println("RIGHT");
                sprite.update(delta);
                x += delta * 0.1f;
            } else if (curr_x - prev_x < 0) { //LEFT
                System.out.println("LEFT");
                sprite.update(delta);
                x -= delta * 0.1f;
            } else if (curr_y - prev_y > 0) { //DOWN
                System.out.println("DOWN");
                sprite.update(delta);
                y += delta * 0.1f;
            } else if (curr_y - prev_y < 0) { //UP
                System.out.println("UP");
                sprite.update(delta);
                y -= delta * 0.1f;
            }

            prev_x = curr_x;
            prev_y = curr_y;
            System.out.println("------------------");
        }

    }
    //End Automated Movement Code

    //Begin KeyPress Enabled Movement Code
    if (input.isKeyDown(Input.KEY_UP)) {
        sprite = up;
        if (!isBlocked(x, y - delta * 0.1f)) {
            sprite.update(delta);
            // The lower the delta the slowest the sprite will animate.
            y -= delta * 0.1f;
        }
    } else if (input.isKeyDown(Input.KEY_DOWN)) {
        sprite = down;
        if (!isBlocked(x, y + SIZE + delta * 0.1f)) {
            sprite.update(delta);
            y += delta * 0.1f;
        }
    } else if (input.isKeyDown(Input.KEY_LEFT)) {
        sprite = left;
        if (!isBlocked(x - delta * 0.1f, y)) {
            sprite.update(delta);
            x -= delta * 0.1f;
        }
    } else if (input.isKeyDown(Input.KEY_RIGHT)) {
        sprite = right;
        if (!isBlocked(x + SIZE + delta * 0.1f, y)) {
            sprite.update(delta);
            x += delta * 0.1f;
        }
    }
    //End KeyPress Enabled Movement Code
}

编辑2

自动移动正在运行,但路径查找器并没有看到&#34;任何阻塞的路径。只是从A点到B点的直线

  @Override
public void update(GameContainer container, int delta) {
    Input input = container.getInput();
    input.enableKeyRepeat();
    //On UserClick, get End Coordinates for PathFinding
    if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
        final_x = input.getMouseX();
        final_y = input.getMouseY();
        circle = new Circle(final_x, final_y, 10);
    }

    if (input.isKeyPressed(Input.KEY_X)) {
        try {
            PropertyBasedMap pbm = new PropertyBasedMap(grassMap, "blocked");
            AStarPathFinder pathFinder = new AStarPathFinder(pbm, max_length, false);
            Path path = pathFinder.findPath(null, (int) x / 10, (int) y / 10, final_x / 10, final_y / 10);
            int length = path.getLength();
            System.out.println("Found path of length: " + length + ".");

            for (int i = 0; i < length; i++) {
                curr_x = path.getX(i) * 10;
                curr_y = path.getY(i) * 10;

                System.out.println("Move From: " + (int) prev_x + "," + (int) prev_y + ".");
                System.out.println("Move To: " + (int) curr_x + "," + (int) curr_y + ".");

                if (curr_x - prev_x > 0) { //RIGHT
                    System.out.println("RIGHT");
                    sprite.update(delta);
                    x += delta * 0.1f;
                } else if (curr_x - prev_x < 0) { //LEFT
                    System.out.println("LEFT");
                    sprite.update(delta);
                    x -= delta * 0.1f;
                } else if (curr_y - prev_y > 0) { //DOWN
                    System.out.println("DOWN");
                    sprite.update(delta);
                    y += delta * 0.1f;
                } else if (curr_y - prev_y < 0) { //UP
                    System.out.println("UP");
                    sprite.update(delta);
                    y -= delta * 0.1f;
                }

                prev_x = curr_x;
                prev_y = curr_y;
                System.out.println("------------------");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    //End Automated Movement Code
}

0 个答案:

没有答案