Sprite not on position

你说的曾经没有我的故事 提交于 2019-12-12 04:47:36

问题


my sprites are not on position on diffrent screen sizes. What i have to do?

My headline actor looks like:

package actors;

public class Headline extends Actor{

 public static final int HEADLINE_POS_X = 450;
 public static final int HEADLINE_POS_Y = 200;


 private final TextureRegion textureRegion;
 private Rectangle textureRegionBounds;
 private OrthographicCamera cam;


public Headline(TextureRegion textureRegion,OrthographicCamera cam) {
    this.textureRegion = textureRegion;
    this.cam = cam;
    Vector3 vec = new Vector3(Constants.HEADLINE_POS_X,Constants.HEADLINE_POS_Y,0);
    cam.unproject(vec);
    textureRegionBounds = new Rectangle(vec.x,vec.y , textureRegion.getRegionWidth(), textureRegion.getRegionHeight());
}

 @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        batch.draw(textureRegion, textureRegionBounds.x, textureRegionBounds.y,800,150);
 }
}

i call in in my screen class.


回答1:


Use viewport for your requirement, I supposed APP_WIDTH and APP_HEIGHT is constant.

public class MenuScreen implements Screen {

  public MenuScreen(Main game) {
     ViewPort viewPort=new ExtendViewport(Constants.APP_WIDTH,Constants.APP_HEIGHT);
     this.stage = new Stage(viewPort);
     this.game = game;
     this.sb = game.sb;
     setHeadline();
  }

  private void setHeadline() {
     atlas = new TextureAtlas(Gdx.files.internal(Constants.HEADLINES_IMAGE_ATLAS_PATH));
     stage.addActor(new Headline(atlas.findRegion(Constants.MENUHEADLINE_REGION_NAMES)));
  }

  @Override
  public void resize(int width, int height) {
    stage.getViewport().update(width,height);
  }
}

HeadLine is an Actor having TextureRegion, why not you use Image for your requirement, if you want some extra property then extend your HeadLine with Image class.

Image image=new Image(new Texture("badlogic.jpg"));
image.setPosition(100,100);   // positon fit for all device screen.
stage.addActor(image);

EDIT

public class Headline extends Image {

   public Headline(TextureRegion textureRegion) {
       super(textureRegion);
   }
}

And In setHeadline() method of MenuScreen

stage.addActor(new Headline(atlas.findRegion(Constants.MENUHEADLINE_REGION_NAMES)));


来源:https://stackoverflow.com/questions/44030842/sprite-not-on-position

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