Sprite flicker while moving libgdx

别来无恙 提交于 2020-01-03 05:08:49

问题


This code makes image Border flicker(Flash) while moving left,right,down or up. Why image border flash wile moving even if I use Screen class render() method delta value.

    package com.me.mygdxgame;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Input.Keys;
    import com.badlogic.gdx.InputProcessor;
    import com.badlogic.gdx.graphics.GL10;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Texture.TextureFilter;
    import com.badlogic.gdx.graphics.g2d.Sprite;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.math.Vector3;
    public class MoveSpriteExample extends GdxTest implements InputProcessor  {
        Texture texture;
        SpriteBatch batch;
        OrthographicCamera camera;
        Vector3 spritePosition = new Vector3();
        Sprite sprite;
        public void resize (int width, int height) {
        }

        public void create() {

            float w = Gdx.graphics.getWidth();
            float h = Gdx.graphics.getHeight();
            batch = new SpriteBatch();
            camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
            camera.setToOrtho(false, w, h);
            texture = new Texture(Gdx.files.internal("data/grasswall.png"));
            texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
            sprite = new Sprite(texture);

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

            sprite.setPosition(spritePosition.x,spritePosition.x);
           lastUpdateTime = System.nanoTime();
        }

    public void Update(float Delta)
    {
        if (Gdx.input.isKeyPressed(Keys.D)==true)
        {
            spritePosition.x += 150*(Delta / 1000000000.0);

        }else if (Gdx.input.isKeyPressed( Keys.A)==true)
        {
            spritePosition.x -= 150*(Delta / 1000000000.0);
        }
        else if (Gdx.input.isKeyPressed( Keys.Z)==true)
        {
            spritePosition.y -= 150*(Delta / 1000000000.0);
        }
        else if (Gdx.input.isKeyPressed( Keys.W)==true)
        {
            spritePosition.y += 150*(Delta / 1000000000.0);
        }
    }
    float lastUpdateTime;
    float currentTime;
        public void render() {
            currentTime = System.nanoTime();
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

            camera.update();

            Update(currentTime - lastUpdateTime);

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

            batch.setProjectionMatrix(camera.combined);

            batch.begin();

            sprite.draw(batch);

            batch.end();

            lastUpdateTime = currentTime;
        }
    }

pls provide code with sample


回答1:


As i said in the last post of you, you should take a look at the Tutorial! First of all you do not need to calculate the delta time yourself. I already postet how you get it in the last post for you.

Ill give you a super short example with a moving sprite without flickering. At first here is the ApplicationListener. (not a GDXtest)

public class MainClass implements ApplicationListener {
    private Screen currentScreen = null;

    @Override
    public void create() {
        Texture.setEnforcePotImages(false);
        this.currentScreen = new TestScreen();
    }

    @Override
    public void dispose() {
        this.currentScreen.dispose();

    }

    @Override
    public void render() {
        this.currentScreen.render(Gdx.graphics.getDeltaTime());
    }

    @Override
    public void resize(int width, int height) {
        this.currentScreen.resize(width, height);
    }

    @Override
    public void pause() {
        this.currentScreen.pause();
    }

    @Override
    public void resume() {
        this.currentScreen.resume();
        ;
    }
}

It's preaty simple and as you can see it does call the render of the screen automatically with the delta time! this.currentScreen.render(Gdx.graphics.getDeltaTime()).

The next thing you need is a simple Screen. So something that does implement the Screeninterface. Id really recommand that you use a Scene2D setup with a stage. But here is an example without.

public class TestScreen implements Screen {
    private Sprite mySprite;
    private SpriteBatch batch = new SpriteBatch();

    public TestScreen() {
        this.mySprite = new Sprite(new Texture(
                Gdx.files.internal("data/appicon.png")));
        this.mySprite.setPosition(50f, 50f);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        this.update(delta);
        this.batch.begin();
        this.mySprite.draw(batch);
        this.batch.end();
    }

    public void update(float delta) {
        if (Gdx.input.isKeyPressed(Keys.D) == true) {
            mySprite.setX(mySprite.getX() + 150 * delta);
        } else if (Gdx.input.isKeyPressed(Keys.A) == true) {
            mySprite.setX(mySprite.getX() - 150 * delta);
        } else if (Gdx.input.isKeyPressed(Keys.Z) == true) {
            mySprite.setY(mySprite.getY() - 150 * delta);
        } else if (Gdx.input.isKeyPressed(Keys.W) == true) {
            mySprite.setY(mySprite.getY() + 150 * delta);
        }
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

Regardas and do take a look at the tutorial. (Here is again the beginning of it)



来源:https://stackoverflow.com/questions/18997013/sprite-flicker-while-moving-libgdx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!