What to do in Screen after each draw and before a new draw?

Hi guys. :stuck_out_tongue: :stuck_out_tongue:
Im trying to make a little plane move on screen and learn about this lib and hmm, i got the same problem that i had in java2D and i wanted to know what should i do :

Each time it passes render it moves the sprite BUT, it gets dragged and it doesnt erase the texture that was before unless this code is there :
:point:

http://imageshack.us/a/img209/1107/withoutclearcode.jpg

http://imageshack.us/a/img822/2448/withclearcode.jpg

::slight_smile: ::slight_smile: ::slight_smile: ::slight_smile:

package mainLoop;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import models.Plane;

/**
 *
 * @author André 
 */
public class ShooterGame implements ApplicationListener {

    // constant useful for logging
    public static final String LOG = ShooterGame.class.getSimpleName();
    // a libgdx helper class that logs the current FPS each second
    private FPSLogger fpsLogger;
    private Plane plane;

    public ShooterGame() {
        plane = new Plane();
    }

    @Override
    public void create() {
        fpsLogger = new FPSLogger();
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    @Override
    public void render() {
        fpsLogger.log();
        update();
       
        // the following code clears the screen with the given RGB color (green)
        Gdx.gl.glClearColor( 0f, 1f, 0f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
        
        Texture planeTexture = plane.getPlaneTexture();
        draw(planeTexture,plane.getxPos(),plane.getyPos());
    }

    public void update()
    {
        plane.setxPos(plane.getxPos() + 2);
        plane.setyPos(plane.getyPos() + 2);
    }
    
    public void draw(Texture texture,int xPos,int yPos)
    {
        Sprite sprite = new Sprite(texture);
        sprite.setX(xPos);
        sprite.setY(yPos);
        //
        SpriteBatch spriteBatch = new SpriteBatch();
        spriteBatch.begin();

        //
        sprite.draw(spriteBatch);
        spriteBatch.flush();
        spriteBatch.dispose();
        //
        spriteBatch.end();
        
    }
    
    
    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

package imagesSounds;

import com.badlogic.gdx.graphics.Texture;

/**
 *
 * @author André 
 */

public class TexturesLoader 
{

/**Chamado Apenas pelas Models*/
public static Texture getPlaneTexture()
{
    Texture splashTexture = new Texture( "D:\\Arquivos && Java\\MeusProjetosJava\\ImagesVideosAudio\\Images\\plane.jpg" );
    return splashTexture;
}
    
    
    
}

Hu uh. Then?

That piece of code will always be there, needed, and you can find it on every libgdx (normal) games because it cleans the screen for you.

Hi m8, ^^ Ty for the answer :smiley:

So im doing it correctly? Im wondering how i would do if it had a background image…

You can draw as many layers as you want, but we all in the world are always clear the screen first because rule of nature :smiley:

  1. delete entire screen (that code)
  2. draw your BG
  3. draw your moving object

Without clearing screen, you will use a same, filled canvas to draw things. If something changes, you have to delete entire screen and redraw. You shouldn’t worry about performance of drawing BG repeatly, the library and GL will have it done for you.

Yay
Now i understand.
Just one question, i need draw the moving object in top of the image? like , i need get the image and draw in top of it ?

Yes, like you did on Java2D.

Ah! Its ok then :stuck_out_tongue:

Thanks m8 :stuck_out_tongue: :smiley: