libgdx桌面应用程序中的对象图像闪烁 [英] Flickering object image movement in libgdx desktop app

查看:110
本文介绍了libgdx桌面应用程序中的对象图像闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码使图像在向左,向右,向下或向上移动时闪烁.

This code makes image flicker while moving left,right,down or up.

public class MoveSpriteExample extends GdxTest implements InputProcessor  {
    Texture texture;
    SpriteBatch batch;
    OrthographicCamera camera;
    Vector3 spritePosition = new Vector3();
    Sprite sprite;

    public void create() {

        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
        batch = new SpriteBatch();
        camera = new OrthographicCamera();
        camera.setToOrtho(false, w, h);

        texture = new Texture(Gdx.files.internal("data/TestImg.png"));

        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        sprite = new Sprite(texture);

        sprite.setSize(32, 32);
        spritePosition.y=100;

        sprite.setPosition(spritePosition.x,spritePosition.x);

    }

    public void render() {
        // set the clear color and clear the screen.
        Gdx.gl.glClearColor(1,1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    //      camera.apply(Gdx.gl10);

        sprite.setPosition(spritePosition.x,spritePosition.y);


        batch.begin();
        sprite.draw(batch);
        batch.end();


        if (Gdx.input.isKeyPressed(Keys.D)==true)
        {
            spritePosition.x += 2;
        }else if (Gdx.input.isKeyPressed( Keys.A)==true)
        {
            spritePosition.x -= 2;
        }
        else if (Gdx.input.isKeyPressed( Keys.Z)==true)
        {
            spritePosition.y -= 2;
        }
        else if (Gdx.input.isKeyPressed( Keys.W)==true)
        {
            spritePosition.y += 2;
        }

        //  camera.unproject(spritePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0));
        // if a finger is down, set the sprite's x/y coordinate.

    }
}

推荐答案

是的,它确实会闪烁,因为每帧将其移动2px.那就像是每秒120像素.您需要根据增量时间移动它. (并且在不发烟的情况下始终为2px)

Yes it does flicker because you move it 2px every frame. Thats like 120px in a second. You need to move it depending on the delta time. (and its always 2px at the time thats not smothy)

例如,您想将其每秒移动50px到按下的方向,您可以像这样进行操作.

For example you want to move it 50px every second into the pressed direction you do it like this.

spritePosition.y += 50f*Gdx.graphics.getDeltaTime();

您确实会根据时间而不是帧速率来更新所有内容.增量时间是最后一帧与该帧之间的时间.

You do update everything depending on the deltatime not the framerate. The deltatime is the time between the last Frame and this frame.

通常,您将使用屏幕来创建这样的设置.如果设置了屏幕,则将使用增量时间调用render方法. (界面中的方法)

Regular you would use a Screen to create a setup like this. If a screen is set the render method does get called with a delta time. (the method from the interface)

您应该看一下 steigert教程.它确实为您提供了一个非常不错的libgdx入门.祝你好运.

You should take a look at the steigert tutorial. It does give you a really great start with libgdx. Good luck with it.

这篇关于libgdx桌面应用程序中的对象图像闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆