我的精灵在不同的屏幕尺寸上没有位置。我该怎么做?
我的标题演员看起来像:
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);
}
}
我在我的屏幕课上打电话。
答案 0 :(得分:1)
根据您的要求使用视口,我认为APP_WIDTH
和APP_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)));