相机不跟随玩家 [英] Camera does not follow player

查看:217
本文介绍了相机不跟随玩家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用libgdx有一个播放器,在移动的 X 方向从左至右。现在,我希望相机跟着它(如在笨鸟先飞为例)。

什么发生的是,球员走出去关闭屏幕,当它到达屏幕的右侧边框和摄像头不跟他走。

我曾尝试以下选项,但没有一次成功:


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

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

  • 的Vector3的Vector3 = camera.unproject(新的Vector3(player.getX(),0F,0F));
    player.setX(vector3.x);

我知道这个问题已经如此就存在,但没有答案在这种情况下工作。也许我错过了,我不知道的东西很重要。


在code:

Game.java类

 公共类游戏扩展com.badlogic.gdx.Game实现ApplicationListener {
  公共静态Vector2 VIEWPORT =新Vector2(320,480);
  公共静态INT宽度;
  公共静态INT HEIGHT;
@覆盖
公共无效创建(){
    WIDTH = Gdx.graphics.getWidth();
    HEIGHT = Gdx.graphics.getHeight();
    // VIEWPORT =新Vector2(宽度/ 2,高度/ 2);
    VIEWPORT =新Vector2(宽度,高度);
    setScreen(新GameScreen(本));
 }@覆盖
公共无效调整大小(INT宽度,高度INT){
    // TODO自动生成方法存根
    super.resize(宽度,高度);
}@覆盖
公共无效简历(){
}@覆盖
公共无效暂停(){
}}

GameScreen.java类

 进口android.util.Log;
进口com.badlogic.gdx.Gdx;
进口com.badlogic.gdx.Input;
进口com.badlogic.gdx preferences。
进口com.badlogic.gdx.Screen;
进口com.badlogic.gdx.audio.Sound;
进口com.badlogic.gdx.graphics.Color;
进口com.badlogic.gdx.graphics.GL20;
进口com.badlogic.gdx.graphics.OrthographicCamera;
进口com.badlogic.gdx.graphics.Texture;
进口com.badlogic.gdx.graphics.g2d.SpriteBatch;
进口com.badlogic.gdx.graphics.g2d.TextureAtlas;
进口com.badlogic.gdx.graphics.g2d.TextureRegion;
进口com.badlogic.gdx.math.Vector3;
进口com.badlogic.gdx.scenes.scene2d.Actor;
进口com.badlogic.gdx.scenes.scene2d.Stage;
进口com.badlogic.gdx.scenes.scene2d.ui.Image;
进口com.badlogic.gdx.scenes.scene2d.ui.Skin;
进口com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
进口com.badlogic.gdx.utils.viewport.FitViewport;
进口java.util.Collections中;公共类GameScreen实现屏幕{私人OrthographicCamera摄像头;私人Player播放器;
私人PlayerInputHandler inputHandler1,inputHandler2;
私人的声音的声音;FitViewport viewp;公共静态INT宽度;
公共静态INT HEIGHT;
INT width_spacing = 0;
INT height_spacing = 0;
阶段阶段;
皮肤的皮肤;
公共GameScreen(Game游戏){
   舞台=新的阶段(新FitViewport(Game.WIDTH,Game.HEIGHT));
    相机=(OrthographicCamera)stage.getCamera();     Gdx.input.setInputProcessor(阶段);
}
@覆盖
公共无效显示(){
    resetGame();
}公共无效resetGame(){
    WIDTH = Gdx.graphics.getWidth();
    HEIGHT = Gdx.graphics.getHeight();
    width_spacing = Game.WIDTH / 24;
    height_spacing = Game.HEIGHT / 14;
    stage.clear();
      皮肤=新的皮肤(​​Gdx.files.internal(数据2 / uiskin.json));    prepareInputHandlers();
    prepare_stage();
}公共无效addPlayer(){
    纹理纹理=新的纹理(player.png);
    玩家=新播放器(纹理);
    player.setPosition(Game.WIDTH / 2,Game.HEIGHT * 2/3);    stage.addActor(播放器);
}
@覆盖
公共无效调整大小(INT宽度,高度INT){    。stage.getViewport()更新(宽度,高度,真);
}@覆盖
公共无效渲染(浮动三角洲){
    Gdx.gl.glClearColor(1,1,1,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    如果(△→1 / 60F){
        player.setX(player.getX()+(4 *增量));
               camera.position.set(player.getX(),camera.position.y,0);    }    更新();
    stage.act(增量);
    stage.draw();
}私人无效更新(){    camera.update();
}私人无效prepareInputHandlers(){    inputHandler2 =新PlayerInputHandler(播放器,Input.Keys.LEFT,Input.Keys.RIGHT,Input.Keys.UP,Input.Keys.DOWN);
}
@覆盖
公共无效的Dispose(){
    sound.dispose();    player.getTexture()处理()。
}
公共无效prepare_stage(){
     addPlayer();    player.setWidth(64);
    player.setHeight(64);
}
@覆盖
公共无效简历(){
    // TODO自动生成方法存根}@覆盖
公共无效的hide(){
    // TODO自动生成方法存根}@覆盖
公共无效暂停(){
    // TODO自动生成方法存根}
 }

Player.java类

 进口com.badlogic.gdx.graphics.Texture; 公共类播放器扩展com.badlogic.gdx.scenes.scene2d.ui.Image {私人纹理质感;
公众播放器(纹理贴图){
    超(纹理);  }
     公共纹理getTexture(){
    返回质感;
}
   @覆盖
   公共无效的行为(浮动三角洲){
    super.act(增量);
   }
  }


解决方案

尝试使用阶段的摄像头,而不是创建自己的。更改GameScreen的构造是这样的:

 公共GameScreen(游戏游戏)
    {
        舞台=新的阶段(新FitViewport(Game.WIDTH,Game.HEIGHT));
        相机=(OrthographicCamera)stage.getCamera();        Gdx.input.setInputProcessor(阶段);
    }

然后在渲染方式设置摄像机的位置,就像

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

camera.update()调用


还有一些奇怪的事情在你的code - 为什么你的 camera.position.set()并设置播放器的 X 在这种情况下:

 如果(增量大于1 / 60F)//?
    {
        player.setX(player.getX()+(4 *增量));
        camera.position.set(player.getX(),camera.position.y,0);
    }

我是pretty确定你不需要这一点 - 但即使你所需要的。

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

应该出if语句,因为现在发生的事情是,即使你正在改变你的球员的位置(使用时,键盘通过的 PlayerInputHandler 对象),你**不更新相机的位置。尝试删除if语句或至少做这样的事情。

 公共无效渲染(浮动三角洲)
    {
        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);        如果(△→1 / 60F)
        {
            player.setX(player.getX()+(100 *三角洲));
        }        camera.position.set(player.getX(),camera.position.y,0);
        更新();        stage.act(增量);
        stage.draw();
    }

最后一件事是,如果你的舞台是空的(不包括播放器)时,相机将开始跟随玩家 - 你会看到当球员不动在所有:)添加更多的成才上演

I use libgdx have one player which moves in x direction from left to right. Now I want the camera to follow it (like in Flappy Bird for example).

What happen is that the player go out off screen when it reaches the right border of screen and the camera don't follow him.

I have tried following options but none of them worked:

  • 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);

I know that this question already exists on SO but none answer works in this case. Maybe I miss something important that i don't know.


The code:

Game.java class

  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 class

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 class

 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);
   }


  }

解决方案

try to use stage's camera instead of creating your own. Change your GameScreen constructor like this:

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

        Gdx.input.setInputProcessor(stage);
    }

then in the render method set the camera position just like

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

before camera.update() call


There is also something strange in your code - why you have camera.position.set() and setting player x in this condition:

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

I'm pretty sure you don't need this - but even if you need the

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

should be out of if statement because what is happening now is that even if you are changing your player position (using keybord through PlayerInputHandler object) you **don't update camera position. Try to remove if statement or at least do something like

    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();
    }

last thing is that if your stage is empty (excluding player) when camera will start to follow player - you will see as player not moving at all :) Add someting more to stage

这篇关于相机不跟随玩家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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