gpt4 book ai didi

java - Libgdx:平移 Scene2d 相机

转载 作者:行者123 更新时间:2023-12-01 12:06:24 25 4
gpt4 key购买 nike

我正在使用 Libgdx 开发一款基于图 block 的游戏,该游戏使用 WASD 移动 map 。我正在使用 scene2d 功能来绘制屏幕,​​并且我正在渲染的 TiledMap 显示得很好。但是当我尝试调用 stage.getCamera().translate(float x, float y, float z); 时方法使用 Gdx.Input.IsKeyPressed() 方法移动视口(viewport),视口(viewport)不会平移。该方法正在被调用,因为“KEY PRESSS”正在输出到控制台,但相机没有转换。还有其他方法来移动视口(viewport)吗? Stage 类是否有其他方法来移动相机?

这是我的屏幕来源:

package com.BurntToast.SolidDiamond;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.viewport.FitViewport;


public class PlayScreen implements Screen {



private MainFrame mainFrame;

private OrthographicCamera camera;
private Vector3 touchCoord;
private TiledMap currentMap;
private OrthogonalTiledMapRenderer otmr;

private ConveyorActor convActor;

private Stage mapStage;


public PlayScreen(MainFrame passedGame){
mainFrame = passedGame;

convActor = new ConveyorActor(mainFrame.conveyorFrames, 1, 0, 0);
mapStage = new Stage();
mapStage.addActor(convActor);
currentMap = new TmxMapLoader().load("maps/MenuMap2.tmx");
otmr = new OrthogonalTiledMapRenderer(currentMap);
otmr.setView((OrthographicCamera)mapStage.getCamera());

}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
//ERIC (erase, redraw, input, calculate)
//ERASE
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

//camera.update();

//REDRAW
otmr.render();
mapStage.draw();


//INPUT/CALCULATE
mapStage.act();
if(Gdx.input.isTouched()){

}//end if touched

//MAP TRANSLATION INPUT HERE:
if(Gdx.input.isKeyPressed(Keys.W)){
//heres the line of code but it won't translate.
mapStage.getCamera().translate(0, 10, 0);
mapStage.getCamera().update();
System.out.println("KEY PRESSS");
}
if(Gdx.input.isKeyPressed(Keys.A)){
mapStage.getCamera().translate(-10, 0, 0);
mapStage.getCamera().update();
}
if(Gdx.input.isKeyPressed(Keys.S)){
mapStage.getCamera().translate(0, -10, 0);
mapStage.getCamera().update();
}
if(Gdx.input.isKeyPressed(Keys.D)){
mapStage.getCamera().translate(10, 0, 0);
mapStage.getCamera().update();
}
}



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

}

@Override
public void show() {
// TODO Auto-generated method stub
mapStage = new Stage(new FitViewport(mainFrame.SCREEN_WIDTH, mainFrame.SCREEN_HEIGHT));
}

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

}

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

}

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

}

@Override
public void dispose() {
// TODO Auto-generated method stub
currentMap.dispose();
}

最佳答案

在 PlayScreen 的构造函数中,您创建一个 mapStage ,它将拥有自己的视口(viewport)和相机。然后将该相机提供给平铺 map 渲染器。

但随后在 show() 中,您创建一个新的 mapStage ,它将替换在构造函数中创建的引用。因此,当您在 render() 中更改相机时,它是新阶段的相机,而不是平铺 map 渲染器仍在使用的旧相机。

无论如何,您都需要避免在 show 方法中使用 new Stage(...),因为这样您每次都会创建一个新的 Stage屏幕更改,这将浪费时间创建新的 Sprite 批处理(占用大量内存并每次编译着色器)。每次替换的旧阶段都会泄漏,因为在其引用丢失之前没有调用 dispose()

我不确定您的限制,但一种解决方案是将您的 show 方法修改为:

public void show() {
mapStage.setViewport(new FitViewport(mainFrame.SCREEN_WIDTH, mainFrame.SCREEN_HEIGHT));
otmr.setView((OrthographicCamera)mapStage.getCamera());
}

还有一些其他的事情...您应该在 dispose() 中处置您的舞台,以避免潜在的泄漏(如果您不这样做,当屏幕发生变化时,舞台中的 Sprite 批处理将泄漏着色器)。并且您的相机移动应始终涉及乘以 delta,因此移动速度与帧速率无关。

关于java - Libgdx:平移 Scene2d 相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27560783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com