精灵不在位置上

时间:2017-05-17 16:57:51

标签: java android libgdx

我的精灵在不同的屏幕尺寸上没有位置。我该怎么做?

我的标题演员看起来像:

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);
 }
}

我在我的屏幕课上打电话。

1 个答案:

答案 0 :(得分:1)

根据您的要求使用视口,我认为APP_WIDTHAPP_HEIGHT是不变的。

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是Actor TextureRegion,为什么不使用Image作为您的要求,如果您想要一些额外的属性,那么使用Image类扩展您的HeadLine。< / p>

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

修改

public class Headline extends Image {

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

setHeadline()

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