简体   繁体   English

如何在JavaFX游戏中创建一个跟随玩家的视图?

[英]How do I create a view following the player in a JavaFX game?

I'm trying to make my first game in JavaFX, where a player object (not a Node) will move inside a level. 我正在尝试在JavaFX中制作我的第一个游戏,其中玩家对象(不是节点)将在一个级别内移动。 However, the full level shouldn't be visible inside the window, only a part of it surrounding the player should be. 但是,窗口内不应显示完整级别,只有播放器周围的一部分应该是。 I've searched quite a bit for a solution to this that works with the way I've done my project, but haven't been successful. 我已经搜索了相当多的解决方案,这与我完成项目的方式一致,但没有成功。 The following code describes how I've set up my GUI: 以下代码描述了我如何设置GUI:

private VBox appRoot;
private Pane gameRoot;
private Canvas canvas;
private GraphicsContext gc;
private HBox uiRoot;

This is the different panes I use. 这是我使用的不同窗格。 The appRoot contains both the gameRoot and the uiRoot, in that order. appRoot按顺序包含gameRoot和uiRoot。 The gameRoot contains the canvas. gameRoot包含画布。 Everything inside the level, including the level itself, is drawn onto the canvas, like this: 关卡中的所有内容(包括关卡本身)都会绘制到画布上,如下所示:

canvas = new Canvas(ROOM_WIDTH, ROOM_HEIGHT);
gc = canvas.getGraphicsContext2D();
createLevel(ROOM_WIDTH, ROOM_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
gc.scale(2, 2);
drawLevel(gc, ROOM_WIDTH, ROOM_HEIGHT, CELL_WIDTH, CELL_HEIGHT);
player.render(gc);

createLevel and drawLevel just creates a level and draws squares where different blocks should be. createLevel和drawLevel只是创建一个级别并绘制不同块应该是的正方形。 The player object's render method draws the player at its x- and y-coordinates. 玩家对象的渲染方法在其x坐标和y坐标处绘制玩家。 As you can see I've scaled the canvas, so as of now the entire level isn't shown when the game is started. 正如你所看到的,我已经缩放了画布,所以到目前为止,游戏开始时并没有显示整个关卡。

The problem is that I can't make it so that what is shown in the window is the things surrounding the player object. 问题是我无法做到这一点,窗口中显示的是玩家对象周围的事物。 I've seen some code utilizing the methods setLayoutX and setLayoutY, but I've been unable to reproduce this so that it works. 我已经看到了一些使用方法setLayoutX和setLayoutY的代码,但是我无法重现它以便它可以工作。

I would be very grateful if anybody could help me out with this, and hopefully without changing too much of how the drawing is done. 如果有人能帮助我解决这个问题,我将非常感激,希望不要过多地改变绘图的方式。

Thanks! 谢谢! :) :)

Also, here are two pictures showing how it looks; 此外,这里有两张图片展示了它的外观; the first is when the player is not inside the current view, and the second is with the player in the view. 第一个是当玩家不在当前视图中时,第二个是视图中的玩家。

Image without player in view 没有球员的图像在视野中

Image with player in view 与球员的图象在视图中

Do not us the Canvas directly to display the level. 不要直接使用Canvas来显示关卡。 Instead you can use an image that represents your level and then have the image in an ImageView . 相反,您可以使用代表您的关卡的图像,然后将图像放在ImageView On that ImageView you can define a viewport of the area of the image that is visible. 在该ImageView您可以定义可见图像区域的视口。

To get the image you can either use your Canvas node and take a snapshot (See this question ). 要获取图像,您可以使用Canvas节点并拍摄快照(请参阅此问题 )。 This is probably the easiest way with what you have. 这可能是你拥有的最简单的方法。 Or you can forego the Canvas altogether and draw the image. 或者你可以完全放弃Canvas并绘制图像。 If the levels are predefined (and not generated at runtime), this seems the best approach, then you can supply the images as resources along with the code. 如果级别是预定义的(并且不是在运行时生成的),这似乎是最好的方法,那么您可以将图像作为资源与代码一起提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM