gpt4 book ai didi

java - libgdx 上的无限滚动背景

转载 作者:行者123 更新时间:2023-11-30 10:21:13 27 4
gpt4 key购买 nike

我正在尝试在 libgdx 上做一个无限滚动的背景。图像在 X 轴上滚动良好,但在 Y 轴上图像被分成两半,如下图所示。我怎样才能让地面从屏幕底部开始? enter image description here

这是我的代码

    package com.tashtoons.game.States;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.tashtoons.game.BuckyRun;
import static com.badlogic.gdx.graphics.Texture.TextureWrap.Repeat;



public class PlaySate extends State {
private Texture bg;
float sourceX = 0;

public PlaySate(GameStateManager gsm) {
super(gsm);
bg=new Texture("bg.png");
cam.setToOrtho(false,BuckyRun.WIDTH/2,BuckyRun.HEIGHT/2);
bg.setWrap(Repeat,Repeat);
}

@Override
protected void handleInput() {

}

@Override
public void update(float dt) {

}

@Override
public void render(SpriteBatch sb) {
sourceX += 10;
sb.setProjectionMatrix(cam.combined);
sb.begin();
sourceX = (sourceX + Gdx.graphics.getDeltaTime() / 3/4) % bg.getWidth();
sb.draw(bg, 0, 0, (int) sourceX, 0,BuckyRun.WIDTH, BuckyRun.HEIGHT);
sb.end();

}

@Override
public void dispose() {

}

}

最佳答案

您应该传递给 cam.setToOrtho() 视口(viewport)宽度和高度。您可能还想更正绘制纹理的代码(在代码中留下注释):

public class PlaySate extends State {
private Texture bg;
float sourceX = 0;

public PlaySate(GameStateManager gsm) {
super(gsm);
bg=new Texture("bg.png");
cam.setToOrtho(false,BuckyRun.WIDTH,BuckyRun.HEIGHT); // edited this
bg.setWrap(Repeat,Repeat);
}


@Override
public void render(SpriteBatch sb) {
sourceX += 10;
sb.setProjectionMatrix(cam.combined);
sb.begin();

// this line doesn't make much sense, you've already incremented sourceX,
// "Gdx.graphics.getDeltaTime() / 3/4" especially confuses me
// why sourceX =% bg.getWidth(); isn't enough?
sourceX = (sourceX + Gdx.graphics.getDeltaTime() / 3/4) % bg.getWidth();

// you probably want to draw texture in full screen
sb.draw(bg,
// position and size of texture
0, 0, WIDTH, HEIGHT,
// srcX, srcY, srcWidth, srcHeight
(int) sourceX, 0, bg.getWidth(), bg.getHeight(),
// flipX, flipY
false, false);
sb.end();

}
}

更新

代替这两行:

sourceX += 10;
...
sourceX = (sourceX + Gdx.graphics.getDeltaTime() / 3/4) % bg.getWidth();
...

应该只有一个:

sourceX = (sourceX + Gdx.graphics.getDeltaTime() * velocity) % bg.getWidth();

关于java - libgdx 上的无限滚动背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47957563/

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