让精灵固定在他们的位置
Make sprites fixate to their positions
我正在 java 制作 Pokemon 的克隆,我有一个 Character main(玩家角色)、Trainers trainer、一个 BufferedImage im 和一个 GraphicsContext gc。我想要完成的是让训练精灵固定在他们特定的地图位置而不是屏幕位置。我的代码适用于水平但不适用于垂直。任何帮助,将不胜感激。我可以根据需要解释更多。其他助手:width 是屏幕宽度,height 是屏幕高度。作为我希望培训师执行的操作的示例:当角色位于地图不再滚动的位置时,它不应随角色移动。否则,它应该(但我不知道怎么做!)
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);
已修复
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);
我正在 java 制作 Pokemon 的克隆,我有一个 Character main(玩家角色)、Trainers trainer、一个 BufferedImage im 和一个 GraphicsContext gc。我想要完成的是让训练精灵固定在他们特定的地图位置而不是屏幕位置。我的代码适用于水平但不适用于垂直。任何帮助,将不胜感激。我可以根据需要解释更多。其他助手:width 是屏幕宽度,height 是屏幕高度。作为我希望培训师执行的操作的示例:当角色位于地图不再滚动的位置时,它不应随角色移动。否则,它应该(但我不知道怎么做!)
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);
已修复
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);