以下是我的问题,当我不移动相机时,我的精灵会很好地跟随我的box2d身体。但是当我又实现滚动时,我使用cam.translate精灵将每个帧都调高,直到消失。 奇怪的是,如果我检查b2body和sprite之间的坐标是否完全相同。
更新: 我尝试使用cam.position.set(cam.position.x,cam.position.y * 1.001f,0)重新放置cam.translate,但我遇到了同样的问题。
变量:
private GalaxyFighter jeu;
//render variables
private OrthographicCamera cam;
private Viewport gamePort;
private Hud hud;
private float maxScrolling;
//tilded map variables
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
//box2d variables
private World world;
private Box2DDebugRenderer b2dr;
private SpaceShip player;
private Sprite spriteSpaceShip;
构造函数:
public EcranJeu(GalaxyFighter jeu, float maxScrolling){
this.jeu = jeu;
cam = new OrthographicCamera();
gamePort = new FitViewport(GalaxyFighter.V_WIDTH / GalaxyFighter.PPM, GalaxyFighter.V_HEIGHT / GalaxyFighter.PPM, cam);
hud = new Hud(jeu.batch);
this.maxScrolling = maxScrolling;
maploader = new TmxMapLoader();
map = maploader.load("level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / GalaxyFighter.PPM);
cam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0,0), true);
b2dr = new Box2DDebugRenderer();
player = new SpaceShip(world);
spriteSpaceShip = new Sprite(new Texture("redship1.png"));
player.b2body.setUserData(spriteSpaceShip);
new B2WorldCreator(world, map);
world.setContactListener(new WorldContactListener());
}
这是我在render中调用的更新方法:
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
if(cam.position.y < (maxScrolling / GalaxyFighter.PPM))
cam.translate(0, 0.5f / GalaxyFighter.PPM);
limitPlayer();
spriteSpaceShip.setPosition(player.b2body.getPosition().x * GalaxyFighter.PPM - 8, player.b2body.getPosition().y * GalaxyFighter.PPM - 8);
System.out.println("Pos player x = " + (player.b2body.getPosition().x * GalaxyFighter.PPM - 8) + " y = " + (player.b2body.getPosition().y * GalaxyFighter.PPM - 8));
System.out.println("Pos sprite x = " + spriteSpaceShip.getX() + " y = " + spriteSpaceShip.getY());
cam.update();
renderer.setView(cam);
}
这是我的渲染方法:
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, cam.combined);
jeu.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
jeu.batch.begin();
spriteSpaceShip.draw(jeu.batch);
jeu.batch.end();
}
我看不到我的错误在哪里,所以我希望有人能找到它! 谢谢您的宝贵时间。
编辑:这是我正在使用的另一个类,我完成了gameScreen类
HUD
public class Hud implements Disposable {
public Stage stage;
private Viewport viewport;
private Integer life;
private static Integer score;
private Integer niveau;
Label lifeLabel;
private static Label scoreLabel;
Label niveauLabel;
Label lifeStrLabel;
Label scoreStrLabel;
Label niveauStrLabel;
public Hud(SpriteBatch sb){
life = 3;
score = 0;
niveau = 1;
viewport = new FitViewport(GalaxyFighter.V_WIDTH, GalaxyFighter.V_HEIGHT, new OrthographicCamera());
stage = new Stage(viewport, sb);
Table table = new Table();
table.top();
table.setFillParent(true);
lifeLabel = new Label(String.format("%02d", life), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
scoreLabel = new Label(String.format("%04d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
niveauStrLabel = new Label("NIVEAU", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
lifeStrLabel = new Label("VIE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
scoreStrLabel = new Label("SCORE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
niveauLabel = new Label(String.format("%02d", niveau), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
table.add(lifeStrLabel).expandX().padTop(10);
table.add(niveauStrLabel).expandX().padTop(10);
table.add(scoreStrLabel).expandX().padTop(10);
table.row();
table.add(lifeLabel).expandX();
table.add(niveauLabel).expandX();
table.add(scoreLabel).expandX();
stage.addActor(table);
}
还有是太空船的玩家:
public class SpaceShip extends Sprite {
public World world;
public Body b2body;
public SpaceShip(World world){
this.world = world;
defineSpaceShip();
}
public void defineSpaceShip(){
BodyDef bdef = new BodyDef();
bdef.position.set(GalaxyFighter.V_WIDTH / 2 / GalaxyFighter.PPM, 3 / GalaxyFighter.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(5 / GalaxyFighter.PPM);
fdef.filter.categoryBits = GalaxyFighter.SPACESHIP_BIT;
fdef.filter.maskBits = GalaxyFighter.DEFAULT_BIT | GalaxyFighter.ASTEROIDE_BIT |GalaxyFighter.BONUS1_BIT|GalaxyFighter.BONUS2_BIT|GalaxyFighter.BONUS3_BIT|GalaxyFighter.PISTE_BIT|GalaxyFighter.BORDSMAP_BIT;
fdef.shape = shape;
b2body.createFixture(fdef);
EdgeShape head_spaceship = new EdgeShape();
head_spaceship.set(new Vector2(-4 / GalaxyFighter.PPM, 0), new Vector2(4 / GalaxyFighter.PPM, 0));
fdef.shape = head_spaceship;
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData("head_spaceship");
EdgeShape head_spaceship2 = new EdgeShape();
head_spaceship2.set(new Vector2(0, -4 / GalaxyFighter.PPM), new Vector2(0, 4 / GalaxyFighter.PPM));
fdef.shape = head_spaceship2;
fdef.isSensor = true;
b2body.createFixture(fdef).setUserData("head_spaceship2");
}
答案 0 :(得分:0)
您是否尝试过将相机的位置设置为播放器的位置,而不是线性移动它?像这样:
cam.position.set(0, spriteSpaceShip.getY(), 0);