简体   繁体   中英

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. 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:

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. In your Game class, you have a sprite batch that you are applying your camera to. But you never use that sprite batch to draw anything. In your PlayState class, you have another sprite batch that you're using for drawing, but you never apply the camera to it.

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:

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. 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 .

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. 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.

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() .

I also noticed that your PlayState class is instantiating a useless instance of the Game class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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