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

查看:17
本文介绍了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,那不是 smothy)

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();

您确实会根据 deltatime 而不是帧速率来更新所有内容.deltatime 是上一帧和本帧之间的时间.

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

您通常会使用屏幕来创建这样的设置.如果设置了屏幕,则渲染方法确实会以增量时间调用.(来自接口的方法)

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天全站免登陆