libgdx get screen width and height for 2d games -


i barely starting on developing android games. trying animate sprite , have done successfully, draw buttons can start making player move, not in right position when should be. me? looks great in desktop version, not on android phone.

this code main class.

package com.adventcloud.realgame;  public class realgame implements applicationlistener {   private orthographiccamera camera; private spritebatch batch; //private texture background; private animatedsprite playeranimated;  //buttons public static texture leftbutton; public static texture rightbutton; public static texture shootbutton; public static texture jumpbutton;   @override public void create() {           int width = gdx.graphics.getwidth();     int height = gdx.graphics.getheight();      texture.setenforcepotimages(false);      camera = new orthographiccamera();     camera.settoortho(false, 1280, 720);      leftbutton = new texture(gdx.files.internal("leftbutton.png"));      rightbutton = new texture(gdx.files.internal("rightbutton.png"));     shootbutton = new texture(gdx.files.internal("shootbutton.png"));        jumpbutton = new texture(gdx.files.internal("jumpbutton.png"));      batch = new spritebatch();      //background = new texture(gdx.files.internal(""));     texture spaceshiptexture = new texture(gdx.files.internal("spritesheet.png"));     sprite playersprite = new sprite(spaceshiptexture);     playeranimated = new animatedsprite(playersprite);     playeranimated.setposition(1280/2, 0); }  @override public void dispose() {     batch.dispose(); }  @override public void render() {           gdx.gl.glclearcolor(0.95f, 0.95f, 0.95f, 0.95f);     gdx.gl.glclear(gl10.gl_color_buffer_bit);      batch.setprojectionmatrix(camera.combined);     batch.begin();         //batch.draw(background, 0, 0);         playeranimated.draw(batch);         batch.draw(leftbutton, 0, 0, gdx.graphics.getwidth()/4, gdx.graphics.getheight()/6);         batch.draw(rightbutton, gdx.graphics.getwidth()/4, 0, gdx.graphics.getwidth()/4, gdx.graphics.getheight()/6);         batch.draw(shootbutton, (gdx.graphics.getwidth()/4)*2, 0, gdx.graphics.getwidth()/4, gdx.graphics.getheight()/6);         batch.draw(jumpbutton, (gdx.graphics.getwidth()/4)*3, 0, gdx.graphics.getwidth()/4, gdx.graphics.getheight()/6);     batch.end(); }  @override public void resize(int width, int height) { }  @override public void pause() { }  @override public void resume() { } } 

here code animated sprite

package com.adventcloud.realgame;  public class animatedsprite { private static final int frame_cols = 16; private static final int frame_rows = 16;  private static final int idle_cols = 4; private static final int idle_rows = 1;  private sprite sprite; private animation animation; private textureregion[] frames; private textureregion currentframe;  private float statetime;  public animatedsprite(sprite sprite){     this.sprite = sprite;     texture texture = sprite.gettexture();     textureregion[][] temp = textureregion.split(texture, texture.getwidth() / frame_cols, texture.getheight() / frame_rows);     frames = new textureregion[idle_cols * idle_rows];     //animation idling ======================================     int index = 0;     (int = 0; < idle_rows; i++) {         (int j = 1; j <= idle_cols; j++) {             frames[index++] = temp[i][j];         }     }     animation = new animation(0.2f, frames);     statetime = 0f;     //end of idle animation ==================================== }  public void draw(spritebatch spritebatch) {     statetime += gdx.graphics.getdeltatime();     //continue keep looping     currentframe = animation.getkeyframe(statetime, true);      spritebatch.draw(currentframe,  sprite.getx() - 32, sprite.gety() + 128, 128, 128); }  public void setposition(float x, float y){     float widthoffset = sprite.getwidth() / frame_cols;     sprite.setposition(x- widthoffset / 2, y); } } 

this camera made (at least 1 of things). can use custom viewport , scaled fit screen/window. example if want have aspect ration of 16/9 , screen should 80 "worldunits" (maybe meters?) wide use viewport of 80 width , 45 height. use camera = new orthographiccamera(80, 45); creates camera 80 units in width, 45 in height , p(0,0) in middle. doing texture, size of 1, 20px wide , 20px high on 1600*900px screen, , 10px wide , 10px high on 800*450px screen. drawing object @ coordinates p(0,0) draws left lower corner in middle of screen, drawing @ p(-40, 0) draws @ left border. not forget tell spritebatch use camera:

spritebatch.setprojectionmatrix(camera.combined); 

to move camera use camera.translate(deltax, deltay); if move camera or rotate or else, changes matrix don't forget call camera.update().

now can calculate in "worldunits" , scaled fit screen. hope help


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -