繁体   English   中英

相机不在LibGDX中移动?

[英]Camera is not moving in LibGDX?

我正在与朋友们一起进行一个项目,而我的工作就是处理视口和摄影机。 当我对其进行测试时,相机很好并且可以很好地移动,但是现在我们将各个部分放在一起,必须将部分代码移到PlayState类。 现在精灵正在移动,但是相机甚至从一开始就没有设置好,看起来好像无法移动。 这里有什么问题? 这是一些代码:

游戏类别:

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

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


}

}

第二次编辑 OK,我刚刚注意到您有两个不同的SpriteBatch,因此您甚至都没有使用正在使用相机的Sprite批处理进行绘图。 在“游戏”课程中,您将要应用其摄影机的sprite批处理。 但是,您永远不要使用该sprite批处理绘制任何内容。 在PlayState类中,您有另一个用于绘制的Sprite批处理,但从未对其应用摄影机。

PlayState甚至不需要SpriteBatch的成员引用,因此只需将其删除并将Game的批处理引用传递到draw方法中即可:

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

第一次编辑:handleInput方法中,您可以根据触摸输入将dxcx调整相同的量。 但是,你设置的字符位置cx但对于相机添加 dx调用到任何当前的相机位置camera.translate代替camera.setPosition

您将要遇到的另一个问题是,您在移动东西时忽略了增量时间,因此您的移动速度将与游戏的帧速率联系在一起,您无法真正控制它并且不一定总是恒定的。 每次更改头寸时,都应通过将常数乘以增量时间来获得或减去。

尝试使用移动的摄像头来移动UI按钮也是不寻常且容易出错的。 如果您的游戏中有移动摄像机,则应该为UI元素使用单独的静态摄像机,以便可以相对于屏幕定义它们。

顺便说一句,您应该在绘制场景之前更新并应用相机,否则距您想要的相机将有一帧滞后。 而且您不必在spriteBatch.begin()end()之间调用spriteBatch.setProjectionMatrix() end()

我还注意到,您的PlayState类正在实例化Game类的无用实例。

暂无
暂无

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

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