我一直在寻找 10 个小时(字面意思),我已经完成了,我需要问一下.事情是我正在学习如何使用 LibGdx 来编写 Java 游戏.我正在做一个水平太空船游戏.所以,我在这里最糟糕的问题是我不知道如何滚动(我认为 draw 会更好地解释).我想画一个巨大的背景(空间)并让我的 OrthographicCamera 像我的 SpaceShip 一样向右移动,所以它会创建一个带有空间背景的滚动效果.没有敌人,只有屏幕上的船.
I have been looking for 10 hours (literally) and I'm done, I need to ask. Thing is I'm learning How use LibGdx to program Java games. I'm doing a Horizontal Space Ship Game. So, my worst problem here is that I do not know how do scroll (I think draw will explain better). I want to draw a huge background (Space) and make my OrthographicCamera move right like with my SpaceShip, so it will create a Scroll effect with the Space Background. No enemies and nothing but the ship on the screen.
我正在尝试这个:
public void moveCamera(float x,float y){
cam.position.set(x, y, 0);
}
然后我在 WorldRender 的 render() 方法中使用该方法:
Then I use that method in my WorldRender render() method:
public void render(float delta){
ship.Move(delta);
moveCamera(ship.getPosition().x,ship.getPosition().y);
cam.update();
System.out.println(""+cam.position);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
我实际上移动了相机位置(感谢 println,我可以看到),但它在游戏中没有移动,所以 SpaceShip 只是消失在窗口边缘.
I actually move the camera position (I can see that thanks to the println), but It isn't moving in the game, so SpaceShip just disappears by the edge of the window.
我在 spriteBatch.end() 之前也试过这个
I also tried this before spriteBatch.end()
spriteBatch.setProjectionMatrix(camera.combined);
但是当我这样做时,Windows 只显示黑屏,没有船,什么也没有.正如我所说,我很绝望,我看到了很多例子(用鼠标滚动,paralexscrolling 等),但都是高级的,或者与我的代码无关.
but when I do that windows only shows a black screen, no ship, no nothing. As I said, I'm desperate, I see lot of examples (scroll with mouse, paralexscrolling etc) but all are to advanced or just nothing to do with my code.
这就是我画东西的方式.背景和船是 WorldRender 中的纹理.我把背景图像画得很宽,所以我的意图是像我说的那样做一些滚动.就是代码
This is how I draw stuff. Background and ship are textures inside WorldRender. I draw background image very wide, so my intention is do some scrolling over as I said. That's the code
private void loadTextures(){
shipTexture=new Texture(Gdx.files.internal("nave.png"));
background=new Texture(Gdx.files.internal("fondo.jpg"));
}
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x*ppuX,ship.getPosition().y*ppuY, ship.WIDTH*ppuX,ship.HEIGHT*ppuY);
}
public void drawBackground(){
spriteBatch.draw(background, -10*ppuX,0*ppuY, Gdx.graphics.getWidth()*10,Gdx.graphics.getHeight());
}
如果有人想在硬核模式下提供帮助,您可以在这里下载代码
Here you can download the code if someone want to help in hardcore mode
我的代码(不工作)
我终于解决了!
这是我在类名 WorldRenderer 中使用的代码,它具有在 GameScreen 中调用以进行渲染、调整大小等的方法
That's the code I used in a class name WorldRenderer, which have methods that are called within GameScreen for render, resize etc
public WorldRenderer(World world) {
// TODO Auto-generated constructor stub
this.world=world;
this.ship=world.getShip();
this.cam = new OrthographicCamera(CAMERA_WIDTH,CAMERA_HEIGHT);
this.cam.setToOrtho(false,CAMERA_WIDTH,CAMERA_HEIGHT);
this.cam.position.set(ship.getPosition().x,CAMERA_HEIGHT/2,0);
this.cam.update();//actualizamos la camara
spriteBatch=new SpriteBatch();
loadTextures();
}
private void loadTextures(){
shipTexture=new Texture(Gdx.files.internal("nave.png"));
background=new Texture(Gdx.files.internal("fondo.jpg"));
}
public void drawShip(){
spriteBatch.draw(shipTexture,ship.getPosition().x,ship.getPosition().y,10,10);
}
public void drawBackground(){
spriteBatch.draw(background, 0,0, 500,50);
}
public void render(float delta){
ship.Move(delta);
moveCamera(ship.getPosition().x);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
drawBackground();
drawShip();
spriteBatch.end();
}
public void moveCemara(float x){
cam.position.set(x+20,cam.position.y, 0);
cam.update();
}
在 Ship 内部我有这个方法,我在 WorldRenderer 的渲染中调用它来移动它
Inside the Ship I have this method which I call within render in WorldRenderer to move It
public void Move(float delta){
if(Gdx.input.isKeyPressed(Keys.LEFT)) this.position.x -=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.RIGHT)) this.position.x +=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.UP)) this.position.y +=velocity *delta;
if(Gdx.input.isKeyPressed(Keys.DOWN)) this.position.y -=velocity *delta;
}
我还要非常感谢帮助过我的人.我将第一个答案标记为好的答案,但是,混合两者才是真正的解决方案.
Also I want to thanks very much to the people who helped me. I'm marking first answer as the good one, but, mix both was what gave me the real solution.
我在这里留下了一些我遵循的教程,这些教程对新手非常有用
I leave here some tutorials I followed which are pretty good for noobs
这是一个很好的从零开始的教程LiGdxForNoobs
That's a good everything-from-scratching-tutorial LiGdxForNoobs
一个简单的平台游戏平台游戏
一个非常简单的游戏bucketGame
不清楚你画的怎么样?我不确定您是否正确地使用了这种方法.您能否提供您的背景和船舶的详细信息?您能否提供有关背景图像的详细信息,它是您滚动的巨大图像还是您想要在滚动时重复的重复图像?
Its not clear how your drawing? I'm not sure if your doing this approach correctly.. Can you provide details of your background and ship? Can you provide details on you background image, is it a huge image that your scrolling around or is it a repeated image you want to repeat as you scroll?
--编辑--
好的,我想我知道会发生什么.我通常会将相机应用于当前上下文.
ok i think i have an idea what might be up. I would normally apply the camera to the current context.
将以下内容放入您的调整大小中
Place the following in your resize
public void resize(int width, int height) {
cam = new OrthographicCamera(width, height);
cam.translate(width / 2, height / 2, 0);
}
将以下内容放在 render() 的开头
Place the following in the start of your render()
cam.position.set(posX,posY,0);
cam.update();
cam.apply(Gdx.gl10);
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // #14
这将使您有一个清晰的屏幕,原点设置在窗口的左下方.然后你应该先画你的背景
This will make you have a clear screen with the origin set at the bottom left of the window. You should then draw your background first
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
spriteBatch.draw(background,0,0,sizeX,sizeY);
spriteBatch.end()
看看当您移动相机位置 posX 和 posY 时效果如何.然后将您的船添加到组合中
see how that looks as you move your camera position posX and posY. Then add your ship to the mix
-- 更多编辑 ---
-- MORE EDITS ---
然后您可以将 posX 和 posY 计算为
you can then calculate the posX and posY as
posX = defaultOffsetX+shipX
posX = defaultOffsetX+shipX
等等..
无论如何希望这会有所帮助我仍然只是在学习自己,所以这可能不是最好的方法..但它似乎有效.
Anyhow hope this helps I'm still only learning myself so this might not be the best method.. but it seems to work.
这篇关于LibGdx 如何使用 OrthographicCamera 滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!