【问题标题】:Camera does not follow player相机不跟随玩家
【发布时间】:2015-12-12 06:16:54
【问题描述】:

我使用 libgdx 有一个玩家在 x 方向上从左到右移动。现在我希望相机跟随它(例如在 Flappy Bird 中)。

当玩家到达屏幕的右边界并且相机没有跟随他时,会发生这种情况。

我尝试了以下选项,但都没有奏效:

  • camera.position.set(player.getX(), camera.position.y, 0);
  • camera.position.set(player.getX(), 0, 0);
  • Vector3 vector3= camera.unproject(new Vector3(player.getX(), 0f, 0f)); player.setX(vector3.x);

我知道这个问题已经存在于 SO 但在这种情况下没有答案。也许我错过了一些我不知道的重要事情。


代码:

Game.java 类

  public class Game extends com.badlogic.gdx.Game implements   ApplicationListener {
  public static Vector2 VIEWPORT = new Vector2(320, 480);
  public static int WIDTH;
  public static int HEIGHT;


@Override
public void create() {
    WIDTH = Gdx.graphics.getWidth();
    HEIGHT = Gdx.graphics.getHeight();
    // VIEWPORT = new Vector2(WIDTH/2, HEIGHT/2);
    VIEWPORT = new Vector2(WIDTH, HEIGHT);
    setScreen(new GameScreen(this));
 }

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub
    super.resize(width, height);
}

@Override
public void resume() {
}

@Override
public void pause() {
}

}

GameScreen.java 类

import android.util.Log;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import java.util.Collections;

public class GameScreen implements Screen {

private OrthographicCamera camera;

private Player  player;
private PlayerInputHandler inputHandler1, inputHandler2;
private Sound sound;

FitViewport viewp;

public static int WIDTH;
public static int HEIGHT;


int width_spacing = 0;
int height_spacing = 0;


Stage stage;


Skin skin;


public GameScreen(Game game) {
   stage = new Stage(new FitViewport(Game.WIDTH, Game.HEIGHT));
    camera = (OrthographicCamera)  stage.getCamera();

     Gdx.input.setInputProcessor(stage);
}


@Override
public void show() {
    resetGame();
}

public void resetGame() {


    WIDTH = Gdx.graphics.getWidth();
    HEIGHT = Gdx.graphics.getHeight();
    width_spacing = Game.WIDTH / 24;
    height_spacing = Game.HEIGHT / 14;
    stage.clear();
      skin = new Skin(Gdx.files.internal("data2/uiskin.json"));

    prepareInputHandlers();
    prepare_stage();
}



public void addPlayer() {
    Texture texture = new Texture("player.png");
    player = new Player(texture);
    player.setPosition(Game.WIDTH / 2, Game.HEIGHT * 2 / 3);

    stage.addActor(player);
}


@Override
public void resize(int width, int height) {

    stage.getViewport().update(width, height, true);
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    if (delta > 1 / 60f) {
        player.setX(player.getX() + (4 * delta));


               camera.position.set(player.getX(), camera.position.y, 0);



    }

    update();


    stage.act(delta);
    stage.draw();
}

private void update() {

    camera.update();


}

private void prepareInputHandlers() {

    inputHandler2 = new PlayerInputHandler(player, Input.Keys.LEFT, Input.Keys.RIGHT, Input.Keys.UP, Input.Keys.DOWN);
}


@Override
public void dispose() {


    sound.dispose();

    player.getTexture().dispose();
}




public void prepare_stage() {
     addPlayer();

    player.setWidth(64);
    player.setHeight(64);




}
@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}
 }

Player.java 类

 import com.badlogic.gdx.graphics.Texture;

 public class Player extends com.badlogic.gdx.scenes.scene2d.ui.Image{

private Texture texture;


public Player(Texture texture) {
    super(texture);

  }
     public Texture getTexture() {
    return texture;
}
   @Override
   public void act(float delta) {
    super.act(delta);
   }


  }

【问题讨论】:

    标签: android camera libgdx render game-development


    【解决方案1】:

    尝试使用舞台的相机而不是创建自己的相机。像这样更改您的 GameScreen 构造函数:

        public GameScreen(Game game) 
        {
            stage = new Stage(new FitViewport(Game.WIDTH, Game.HEIGHT));
            camera = (OrthographicCamera) stage.getCamera();
    
            Gdx.input.setInputProcessor(stage);
        }
    

    然后在 render 方法中设置相机位置就像

        camera.position.set(player.getX(), camera.position.y, 0);
    

    camera.update() 调用之前


    您的代码中还有一些奇怪的地方 - 为什么您有 camera.position.set() 并在这种情况下设置播放器 x

        if (delta > 1 / 60f) //??
        {
            player.setX(player.getX() + (4 * delta));
            camera.position.set(player.getX(), camera.position.y, 0);
        }
    

    我很确定你不需要这个 - 但即使你需要

        camera.position.set(player.getX(), camera.position.y, 0);
    

    应该超出 if 语句,因为现在发生的情况是,即使您正在更改播放器位置(通过 PlayerInputHandler 对象使用键盘),您**不要更新相机位置。尝试删除 if 语句或至少执行类似的操作

        public void render(float delta) 
        {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            if (delta > 1 / 60f) 
            {
                player.setX(player.getX() + (100 * delta));
            }
    
            camera.position.set(player.getX(), camera.position.y, 0);
            update();
    
            stage.act(delta);
            stage.draw();
        }
    

    最后一件事是,如果你的舞台是空的(不包括玩家),那么相机将开始跟随玩家 - 你会看到玩家根本没有移动 :) 向舞台添加更多内容

    【讨论】:

    • 很抱歉,gameWorld 对象是什么
    • 我所有的代码类都是上面写的,我不知道这个对象
    • 很抱歉,我已经从我的旧代码中复制了它并且没有更改为 stage - 我已经编辑了它
    • 谢谢,但对我来说没有成功,它从一开始就让播放器在屏幕右边框附近几乎静止并且不会移动
    • 请分享整个 Screen 类(游戏一)代码或至少更新问题 - 很难调查没有看到代码的问题... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    相关资源
    最近更新 更多