libgdxtexturessprite2d-gamessprite-sheet

Using multiple textures in libgdx efficiently


I'm new to libgdx and I've been using it for only 2 weeks. My problem is with understanding how texture loading works.

I managed to load my first texture (a player) using gdx.files.internal(player.png) or something along those lines and it worked fine. I added some functionality to make him move side to side with the 'if key pressed' command stuff and that works too.

My problem comes when loading in another texture. I want to make an enemy player, but have minimal knowledge on how to do this. I thought if I did "gdx.files.internal(enemy.png)", it would load in my enemy texture, but it doesn't, instead it loads in another player.png texture.

My question is how do I load in my enemy.png? I have seen some useful tutorials which I followed but it keeps loading my player again and again every time.

It would be helpful if someone could explain how to efficiently use multiple textures because I feel the way I'm doing it isn't exactly the best practice.

    package com.mohamed.JungleFighter;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

//i'm extending libgdx's built in game class which implements the activity listener
public class JungleFighterMain extends Game {
    
    private OrthographicCamera camera;
    private SpriteBatch sBatch;
    private Texture player;
    private Texture enemy;
    //private SpriteBatch enemyBatch;
    private Sprite sprite;
    //just setting my game heighty and width
    public static int gameWidth = 500, gameHeight = 500;


    
    @Override
    public void create () {
        //camera related
        camera = new OrthographicCamera(gameWidth, gameHeight);
        //end of camera related
        sBatch = new SpriteBatch();
        player = new Texture(Gdx.files.internal("plane.png"));
        sprite = new Sprite(player);
        enemy = new Texture(Gdx.files.internal("enemy.png"));
        sprite = new Sprite(enemy);
        

    }
    
    public  void dispose() {
        sBatch.dispose();
        player.dispose();
        enemy.dispose();

    }
    
    

    @Override
    public void render () {
        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        // camera related
           sBatch.setProjectionMatrix(camera.combined);

        
        //keyboard functions
        
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
                sprite.translateX(-1f);
            else
                sprite.translateX(-20.1f);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            if(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
                sprite.translateX(1f);
            else
                sprite.translateX(20.1f);
            
        }

        sBatch.begin();
        //sprite.setPosition(-200, -200);
        sprite.draw(sBatch);
        sBatch.end();
        

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

        public  void pause(){
        
        }
        
        public  void resume(){
            
        }
        
    
    }

Solution

  • I think I found your problem

     player = new Texture(Gdx.files.internal("plane.png"));
        sprite = new Sprite(player);
        enemy = new Texture(Gdx.files.internal("enemy.png"));
        sprite = new Sprite(enemy);
    

    See, you only have one sprite. If you only want to have an enemy load only him, if you want both, make 2 Sprites..

    player = new Texture(Gdx.files.internal("plane.png"));
            sprite1 = new Sprite(player);
            enemy = new Texture(Gdx.files.internal("enemy.png"));
            sprite2 = new Sprite(enemy);
    

    And remember that if you want to draw a texture you can also use

    batch.draw(textureName,xPos,yPos);