LIBGDX平滑玩家运动

时间:2014-01-05 12:40:50

标签: java libgdx

我的游戏(跳线型游戏)有问题,当玩家跌倒或向左/向右移动时它不平滑,我可以多次看到精灵(像某种阴影)。在搜索互联网后,我认为这是deltaTime的东西,但我无法找到如何解决它。 播放屏幕中渲染方法的代码是:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0,0,0,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //game running
    if(state == RUNNING){
        //make world step
        doPhysicsStep(delta);
    //update player and camera
        player.update(delta);
    if(player.getBody().getPosition().x < bottomLeft.x)
        player.getBody().setTransform(bottomRight.x, player.getBody().getPosition().y, player.getBody().getAngle());
    else if(player.getBody().getPosition().x > bottomRight.x)
        player.getBody().setTransform(bottomLeft.x, player.getBody().getPosition().y, player.getBody().getAngle());

    camera.position.y =player.getBody().getPosition().y > camera.position.y ? player.getBody().getPosition().y: camera.position.y;
    camera.update();
    levelGenerator.generate(camera.position.y + camera.viewportHeight /2,camera.position.y - camera.viewportHeight /2);
    }

    //game ended
    if(player.getBody().getPosition().y< camera.position.y - camera.viewportHeight/2 -player.HEIGHT/2 && state == RUNNING){
        gameOverScreen.updateScore(score.getText().toString().substring(7));
        state = END;
    }
    if(state == END){
        gameOverScreen.setVisible(true);
    }
    //drawing game
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    renderBackground();
    renderPlatform();
    renderPlayer();
    Assets.sparks.draw(batch, delta);
    batch.end();
}

public void renderPlatform(){
    //world.getBodies(tmpBodies);
    //for(Body body : tmpBodies)
    //  if(body.getUserData() != null && body.getUserData() instanceof Platform)
            for(int i=0;i<platforms.size();i++){

            Body body = platforms.get(i).getBody();
            Sprite sprite = platforms.get(i).getSprite();
            sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
            sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
            sprite.draw(batch);
        }
}

public void renderPlayer(){
    world.getBodies(tmpBodies);
    for(Body body : tmpBodies)
        if(body.getUserData() != null && body.getUserData() instanceof Player){
            Player player = (Player) body.getUserData();
            Sprite sprite = player.getSprite();
            sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
            //sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
            sprite.draw(batch);
            if(player.isPlayEffect()){
                Assets.sparks.setPosition(body.getPosition().x , body.getPosition().y - sprite.getHeight()/2);
                Assets.sparks.setDuration(200);
                Assets.sparks.start();
            }

        }
}

public void renderBackground(){
    world.getBodies(tmpBodies);
    for(Body body : tmpBodies)
        if(body.getUserData() != null && body.getUserData() instanceof Background){
            Background background = (Background) body.getUserData();
            Sprite sprite = background.getSprite();
            //sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
            sprite.setBounds(bottomLeft.x, camera.position.y - camera.viewportHeight /2, camera.viewportWidth, camera.viewportHeight);
            sprite.draw(batch);
        }

}
private void doPhysicsStep(float deltaTime) {
    // fixed time step
    // max frame time to avoid spiral of death (on slow devices)
    float frameTime = Math.min(deltaTime, 0.05f);
    accumulator += frameTime;
    while (accumulator >= TIMESTEP) {
        world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
        accumulator -= TIMESTEP;
    }
}

和玩家更新:

public void update(float delta) {
    stateTime += delta;
    if(state==JUMP){
        sprite.setRegion(jump.getKeyFrame(stateTime));
        if(jump.isAnimationFinished(stateTime)){
            state = IDLE;
            stateTime = 0;
        }
    }else{
        sprite.setRegion(idle.getKeyFrame(stateTime));
    }
    if(Gdx.app.getType() == ApplicationType.Android )
        velocity.x = -Gdx.input.getAccelerometerX() * (movementForce / 10f);
    if(applyJump){
    body.applyLinearImpulse(0, jumpPower, body.getWorldCenter().x, body.getWorldCenter().y, true);
    applyJump = false;}
    body.setLinearVelocity(velocity.x, body.getLinearVelocity().y);
}

applyJump方法中联系人设置了postSolve()变量。

提前致谢

1 个答案:

答案 0 :(得分:1)

那么,那是两个不同的问题,对吗?它不是很平滑,它不应该绘制多个精灵?

让我们首先解决第二个,这应该是非常明显的。您要么多次调用以在某处渲染精灵,要么您没有正确清除帧。看起来你正在清理它,所以你必须完成所有的渲染代码,看看你是如何绘制两次纹理的。它发生在某处。

world.getBodies(tmpBodies);
    for(Body body : tmpBodies)

为什么这段代码存在?玩家角色有多个精灵吗?我不会这样处理它;这个数字会随着时间而变化吗?是否有新玩家被创建?您应该创建一个单独的Player类,并允许它管理自己的状态和数据。使用OOP原则!这里有太多的GOD课程,非常程序化的风格。坏Java。

如果左/右移动不顺畅,则需要处理物理。