简体   繁体   English

相机不在LibGDX中移动?

[英]Camera is not moving in LibGDX?

I am working on a project with friends and I made my part which was to do with viewports and cameras. 我正在与朋友们一起进行一个项目,而我的工作就是处理视口和摄影机。 When I tested it the camera was fine and moved fine, but now we put our parts together and I had to move some of my code to a PlayState class. 当我对其进行测试时,相机很好并且可以很好地移动,但是现在我们将各个部分放在一起,必须将部分代码移到PlayState类。 Now the sprites are moving, but the camera isn't even set right from the start and it looks like it can't move. 现在精灵正在移动,但是相机甚至从一开始就没有设置好,看起来好像无法移动。 What is the problem here? 这里有什么问题? Here is some code: 这是一些代码:

Game class: 游戏类别:

package com.platformer.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.platformer.managers.GameStateManager;


public class Game implements ApplicationListener{

private GameStateManager gsm;

SpriteBatch batch;

final float WIDTH=480;
final float HEIGHT=320;

float dx,dy;

public Viewport viewport;
public OrthographicCamera camera;

public void create () {

    gsm = new GameStateManager();

    batch = new SpriteBatch();

    float aspectRatio=(float)Gdx.graphics.getWidth()/(float)Gdx.graphics.getHeight();

    camera=new OrthographicCamera();
    viewport=new FitViewport(HEIGHT*aspectRatio,HEIGHT,camera);
    viewport.apply();
}

public void render () {

    //clear frame
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.translate(dx,dy);

    //update and draw gamestate
    gsm.update(Gdx.graphics.getDeltaTime());
    gsm.draw();


    camera.update();

    batch.begin();
    batch.setProjectionMatrix(camera.combined);
    batch.end();


}

public void resize(int width, int height) {

    viewport.update(width,height);
    camera.position.set(WIDTH,HEIGHT/2,0);

}

public void pause() {

}

public void resume() {

}

public void dispose() {

}

public void setCamera(float dx,float dy){

    this.dx=dx;
    this.dy=dy;

}

public float getWIDTH(){return WIDTH;}
public float getHEIGHT(){return HEIGHT;}

}

PlayState class: PlayState类:

package com.platformer.gamestates;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.platformer.game.Game;
import com.platformer.managers.GameStateManager;

public class PlayState extends GameState{

SpriteBatch batch;
Sprite right,left,background,character;

float dx,dy;

Game game=new Game();

float lx=game.getWIDTH()/2+5,ly=5,cx=game.getWIDTH()/2+165,cy=45;

public PlayState(GameStateManager gsm){
    super(gsm);

}

public  void init(){

    dx=0;
    dy=0;
    batch = new SpriteBatch();
    background=new Sprite(new Texture(Gdx.files.internal("happyplace.png")));
    right=new Sprite(new Texture(Gdx.files.internal("go_right.png")));
    left=new Sprite(new Texture(Gdx.files.internal("go_left.png")));
    character=new Sprite(new Texture(Gdx.files.internal("char.png")));

}
public  void update(float dt){

    handleInput();

    left.setPosition(lx,ly);
    right.setPosition(lx+80,ly);
    character.setPosition(cx,cy);

    game.setCamera(dx,dy);
    dx=0;
    dy=0;

}

public  void draw(){

    batch.begin();
    background.draw(batch);
    right.draw(batch);
    left.draw(batch);
    character.draw(batch);
    batch.end();

}

public  void handleInput(){

    //some movement bits
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){dx+=5f;lx+=5f;}
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){dx-=5f;lx-=5f;}
    if(Gdx.input.isKeyPressed(Input.Keys.UP)){dy+=5f;ly+=5f;}
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){dy-=5f;ly-=5f;}

    if(Gdx.input.isTouched()){

        if(Gdx.input.getX()<40
                &&Gdx.input.getY()>game.getHEIGHT()-40){dx-=5f;lx-=5f;cx-=5f;}
        if(Gdx.input.getX()<120
                &&Gdx.input.getX()>80
                &&Gdx.input.getY()>game.getHEIGHT()-40){dx+=5f;lx+=5f;cx+=5f;}

    }

}

public  void dispose(){

    background.getTexture().dispose();
    left.getTexture().dispose();
    right.getTexture().dispose();


}

}

Second Edit OK, I just noticed that you have two different SpriteBatches, so you're not even drawing with the sprite batch that is using your camera. 第二次编辑 OK,我刚刚注意到您有两个不同的SpriteBatch,因此您甚至都没有使用正在使用相机的Sprite批处理进行绘图。 In your Game class, you have a sprite batch that you are applying your camera to. 在“游戏”课程中,您将要应用其摄影机的sprite批处理。 But you never use that sprite batch to draw anything. 但是,您永远不要使用该sprite批处理绘制任何内容。 In your PlayState class, you have another sprite batch that you're using for drawing, but you never apply the camera to it. 在PlayState类中,您有另一个用于绘制的Sprite批处理,但从未对其应用摄影机。

PlayState doesn't even need a member reference to the SpriteBatch, so just remove it and pass the Game's batch reference into your draw method: PlayState甚至不需要SpriteBatch的成员引用,因此只需将其删除并将Game的批处理引用传递到draw方法中即可:

public void draw(SpriteBatch batch){
    //...
}

First Edit: In your handleInput method, you adjust dx and cx by the same amount in response to touch input. 第一次编辑:handleInput方法中,您可以根据触摸输入将dxcx调整相同的量。 However, you set the character position to cx but for the camera you add dx to whatever the current camera position by calling camera.translate instead of camera.setPosition . 但是,你设置的字符位置cx但对于相机添加 dx调用到任何当前的相机位置camera.translate代替camera.setPosition

The other issue you're going to have is that you are ignoring delta time when you move stuff, so your movement speed is going to be tied to the frame rate of the game, which you cannot really control and is not necessarily always constant. 您将要遇到的另一个问题是,您在移动东西时忽略了增量时间,因此您的移动速度将与游戏的帧速率联系在一起,您无法真正控制它并且不一定总是恒定的。 Any time you change a position, you should do it by adding or subtracting some constant multiplied by delta time. 每次更改头寸时,都应通过将常数乘以增量时间来获得或减去。

It's also a bit unusual and error prone to try to move your UI buttons around with the moving camera. 尝试使用移动的摄像头来移动UI按钮也是不寻常且容易出错的。 If your game has a moving camera, you should use a separate static camera for the UI elements so they can be defined in relation to the screen. 如果您的游戏中有移动摄像机,则应该为UI元素使用单独的静态摄像机,以便可以相对于屏幕定义它们。

By the way, you should update and apply the camera before drawing the scene, or there will be a one frame lag from where you want the camera to be. 顺便说一句,您应该在绘制场景之前更新并应用相机,否则距您想要的相机将有一帧滞后。 And you don't have to call spriteBatch.setProjectionMatrix() in between spriteBatch.begin() and end() . 而且您不必在spriteBatch.begin()end()之间调用spriteBatch.setProjectionMatrix() end()

I also noticed that your PlayState class is instantiating a useless instance of the Game class. 我还注意到,您的PlayState类正在实例化Game类的无用实例。

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

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