LibGdx如何使用OrthographicCamera进行滚动? [英] LibGdx How to Scroll using OrthographicCamera?

查看:90
本文介绍了LibGdx如何使用OrthographicCamera进行滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在闲逛10个小时(从字面上看),我已经完成了,我需要问一下.我正在学习如何使用LibGdx对Java游戏进行编程.我在做水平太空飞船游戏.因此,我最严重的问题是我不知道如何滚动(我认为画图会更好地解释).我想绘制一个模糊背景(Space),并使我的OrthographicCamera像我的SpaceShip一样向右移动,因此它将使用Space Background创建一个Scroll效果.屏幕上没有敌人,只有敌人.

I have been loocking 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 hudge background (Space) and make my OrthographicCamera move right like with my SpaceShip, so it will create a Scroll effect with the Space Background. No enemys 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 dissapears by the edge of the window.

我在spriteBatch.end()之前也尝试过此操作

I also tried this before spriteBatch.end()

spriteBatch.setProjectionMatrix(camera.combined);

但是,当我这样做时,窗户却显示出黑屏,没有飞船,什么也没有. 就像我说的那样,我很拼命,我看到了很多例子(用鼠标滚动,副词滚动等等),但是所有例子都是高级的,或者与我的代码无关.任何帮助将不胜感激

but when I do that windows oly 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. Any help wil be appreciated

这就是我画东西的方式.背景和船是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

我的代码(不起作用)

我终于解决了!!! WIIIIIIIIII !!! 那是我在类名WorldRenderer中使用的代码,其中有在GameScreen中被调用的方法,用于渲染,调整大小等

I FINALLY SOLVED IT!!!! WIIIIIIIIII!!! That's the code I used in a class name WorldRenderer, wich have methods that are calle 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();
}

在飞船内部,我拥有在WorldRenderer的render中调用此方法以移动它的方法

Inside the Ship I have this method wich 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 tutos I followed wich are pretty good for noobs

从头到脚都很好 LiGdxForNoobs

一个简单的平台游戏

A simple plataform game platformGame

一个非常简单的游戏 bucketGame

A very simple game 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.

将以下内容调整大小

    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

-更多编辑---

然后您可以将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进行滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆