让精灵固定在他们的位置上

时间:2016-11-08 17:32:58

标签: java animation javafx java-8 sprite

我正在java中修复Pokemon,我有一个角色主角(玩家角色),训练师训练师,BufferedImage im和GraphicsContext gc。我想要完成的是让教练精灵固定到他们特定的地图位置而不是屏幕位置。我的代码适用于水平而非垂直。任何帮助,将不胜感激。我可以根据需要解释更多。其他助手:宽度是屏幕宽度,高度是屏幕高度。作为我希望教练要做的事情的一个例子:当角色处于地图不再滚动的点时,它不应该与角色一起移动。否则,它应该(但我不知道如何!)

main.setPosx(main.getPosx() + main.getSprite().getVelocityX() * t);
main.setPosy(main.getPosy() + main.getSprite().getVelocityY() * t);
for (Trainer trainer : map.getTrainers()) {
    trainer.setPosx(trainer.getPosx() + trainer.getSprite().getVelocityX() * t);
    trainer.setPosy(trainer.getPosy() + trainer.getSprite().getVelocityY() * t);
}
main.getSprite().setPosition(main.getPosx() * (width / im.getWidth()),
        main.getPosy() * (height / im.getHeight()));

/*where the problem is*/
for (Trainer trainer : map.getTrainers()) {
    trainer.getSprite().setPosition(
            ((trainer.getPosx() - (int) (main.getPosx() - width / 4 < 0 ? 0
                    : main.getPosx() > im.getWidth() - width / 4
                            ? im.getWidth() - width / 2
                            : main.getPosx() - width / 4))
                              * (width / im.getWidth())),
            ((trainer.getPosy() - (int) (main.getPosy() - height / 4 < 0 ? 0
                    : main.getPosy() > im.getHeight() - height / 4
                            ? im.getHeight() - height / 2
                            : main.getPosy() - height / 4))
                              * (height / im.getHeight())));
}

gc.clearRect(0, 0, width, height);
gc.drawImage(SwingFXUtils.toFXImage(im.getSubimage((int) (main.getPosx() - width / 4 < 0 ? 0
        : main.getPosx() > im.getWidth() - width / 4 ? im.getWidth() - width / 2
                : main.getPosx() - width / 4),
        (int) (main.getPosy() - height / 4 < 0 ? 0
                : main.getPosy() > im.getHeight() - height / 4 ? im.getHeight() - height / 2
                        : main.getPosy() - height / 4),
        (int) width / 2, (int) height / 2), null), 0D, 0D, width, height);

1 个答案:

答案 0 :(得分:0)

已修复

                trainer.getSprite().setPosition(
                        (trainer.getPosx() - (main.getPosx() < width / 4 ? width / 4
                                : main.getPosx() > im.getWidth() - width / 4 ? im.getWidth() - width / 4
                                        : main.getPosx()))
                                * 2 + width / 2,
                        (trainer.getPosy() - (main.getPosy() < height / 4 ? height / 4
                                : main.getPosy() > im.getHeight() - height / 4 ? im.getHeight() - height / 4
                                        : main.getPosy()))
                                * 2 + height / 2);