Friday, 15 April 2011

using multiple textures in libgdx efficiently -



using multiple textures in libgdx efficiently -

im new libgdx i've been using 2 weeks. problem understanding how texture loading works.

i managed load first texture (a player) using gdx.files.internal(player.png) or along lines , worked fine, added functionality create him move side side if key pressed command stuff , works too.

my problem comes when loading in texture, want create enemy player. have minimal knowledge on how this. thought if did "gdx.files.internal(enemy.png)" load in enemy texture doesn't, instead loads in player.png texture.

my question how load in enemy.png. have seen useful tutorials followed keeps loading player 1 time again , 1 time again every time.

please can help me understand, have been stuck on 3 days

i can show code if needed in case doing wrong. helpful if explain how efficiently utilize multiple textures because sense way i'm doing inst best practice

bundle 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 implements 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 game heighty , width public static int gamewidth = 500, gameheight = 500; @override public void create () { //camera related photographic camera = new orthographiccamera(gamewidth, gameheight); //end of photographic 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); // photographic 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(){ } }

i think found 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, have 1 sprite. if want have enemy load him, if want both, create 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 if want draw texture can use

batch.draw(texturename,xpos,ypos);

libgdx textures sprite 2d-games sprite-sheet

No comments:

Post a Comment