无限滚动纹理

时间:2014-06-04 16:11:27

标签: android libgdx

我正在使用libgdx框架并试图永远重复尖峰的背景。我遇到的问题是纹理永远移动得非常快,但我想减慢速度。我在查看a tutorial from Google Code,另一位用户遇到了同样的问题。 "假设"解决方案是:

if(scrollTimer>1.0f)

    scrollTimer = 0.0f; 

scrollTimer = 3f;

这是为了让它滚动快3倍,所以我尝试了这个并替换了0.3f而不是3f,但这使得背景处于停滞状态。

以下是一些可能有助于解决此问题的相关代码:

public class PlayScreen implements Screen{

    final Rectangle upSpikeBounds = new Rectangle(0,0,Gdx.graphics.getWidth() * 2, 25);
    final Rectangle downSpikeBounds = new Rectangle(0,Gdx.graphics.getHeight() - 25,Gdx.graphics.getWidth() * 2, 25);
    Rectangle blockBounds;
    float total_time = 0f;
    Sprite spikeUpSprite, spikeDownSprite, spriteBlock;
    public PlayScreen(Game game){
        batch = new SpriteBatch();
        player = new Player(new Vector2(Gdx.graphics.getWidth()/10, Gdx.graphics.getHeight()/2), batch);
        stage = new Stage();
        this.game = game;

        Texture spikeUp = new Texture(Gdx.files.internal("spikes.png"));
        Texture spikeDown = new Texture(Gdx.files.internal("spikes.png"));
        spikeUp.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
        spikeDown.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
        spikeUpSprite = new Sprite(spikeUp, 0, 0, Gdx.graphics.getWidth(), 25);
        spikeDownSprite = new Sprite(spikeDown, 0, 25, Gdx.graphics.getWidth(), -25);
        spikeDownSprite.setPosition(0, Gdx.graphics.getHeight() - 20);
        spikeUpSprite.setSize(1200, 25);
        spikeDownSprite.setSize(1200, 25);

    }
    public void render(float deltaTime) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        label.setText("Score: " + player.score);

        cam.position.set(player.getPosition().x + Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight () / 2,0);
        batch.setProjectionMatrix(cam.combined);
        cam.update();

        total_time += Gdx.graphics.getDeltaTime();
        if (total_time > 1.0f){
            total_time = 0.0f;
        }
        spikeUpSprite.setU(total_time);
        spikeUpSprite.setU2((total_time+1));
        spikeDownSprite.setU(total_time);
        spikeDownSprite.setU2(total_time+1);

        stage.act();
        batch.begin();
        spikeUpSprite.draw(batch);
        spikeDownSprite.draw(batch);
        batch.end();
        stage.draw();
    }

}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

您应该查找补间引擎,它将使您能够使用动画'这是一个值的动画,具有持续时间。例如,如果您使用700毫秒的持续时间从0到700进行动画处理,则该值将从零开始,每ms增加1

答案 1 :(得分:0)

虽然micnubinub的答案肯定很有趣并且值得一试,但我已经找到了解决问题的快速方法。问题出在这一行:

total_time += Gdx.graphics.getDeltaTime();

这就是背景速度所在。因为增量时间太快(可能是微秒),所以背景滚动得太快。如果我将其更改为手动编号,例如0.0093f,我会得到一个以我想要的速度滚动的背景。