当我将libGDX摄像机的位置设置为播放器x和y时,为什么播放器会移出屏幕? [英] Why does my player move off the screen when I set the libGDX camera's position to the players x and y?
问题描述
在过去的几天里,我一直在不停地移动,仅用一个摄像头移动播放器,并使摄像头跟随播放器的x和y.当我进行搜索时,我发现的是移动播放器并将相机的x和y设置为该值.但是我有一个问题,我的播放器没有停留在屏幕中间,确实很烦人.如果有人可以帮我,那太好了.这是代码.
for the past few days i have been tinkering around with moving the player with just one camera and having the camera follow the player's x and y. When i searched this up all i found was to move the player and set the camera's x and y to that. But i am having a problem, my player does not stay in the middle of the screen and it is really anoying. If anyone could hep me that would be great. Here is the code.
在创建中
cam = new OrthographicCamera();
cam.setToOrtho(false, Main.WIDTH, Main.HEIGHT);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
在更新中;
cam.position.set(Player.getX() + Main.WIDTH / 2, Player.getY() + Main.HEIGHT / 2, 0);
cam.update();
运动中
if (Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP)) {
Player.setVelY(Player.SPEED);
} else if (Gdx.input.isKeyPressed(Keys.S)
|| Gdx.input.isKeyPressed(Keys.DOWN)) {
Player.setVelY(-Player.SPEED);
} else {
Player.setVelY(0);
}
if (Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) {
Player.setVelX(-Player.SPEED);
Player.dir = Player.Direction.LEFT;
} else if (Gdx.input.isKeyPressed(Keys.D)
|| Gdx.input.isKeyPressed(Keys.RIGHT)) {
Player.setVelX(Player.SPEED);
Player.dir = Player.Direction.RIGHT;
} else {
Player.setVelX(0);
Player.dir = null;
}
推荐答案
我建议您将相机位置设置为播放器Sprite的位置.我不知道您的Player.getX()
返回什么,但是它可能应该返回Sprite表示玩家的x坐标. y坐标也是如此.
I'd suggest you just set the camera position to the position of the Sprite of the player. I can't tell what your Player.getX()
returns but it should probably return the x-coordinate of the Sprite denoting the player. The same goes for the y-coordinate.
cam.position.set(Player.getX() + Main.WIDTH / 2, Player.getY() + Main.HEIGHT / 2, 0);
将会
cam.position.set(Player.getX() + Player.getWidth() / 2, Player.getY() + Player.getHeight() / 2, 0);
这篇关于当我将libGDX摄像机的位置设置为播放器x和y时,为什么播放器会移出屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!