Java 雪碧不在位置上
我的精灵在不同大小的屏幕上不在位置上。我该怎么办 我的头条演员看起来像:Java 雪碧不在位置上,java,android,libgdx,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
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);
}
}
我在屏幕课上打电话。根据您的要求使用视口,我认为
APP\u宽度和APP\u高度是恒定的
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是一个演员
拥有纹理区域
,为什么不根据您的需求使用图像
,如果您需要一些额外的属性,那么可以使用图像
类扩展您的HeadLine
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的method中
stage.addActor(new Headline(atlas.findRegion(Constants.MENUHEADLINE_REGION_NAMES)));
如何创建stage
,显示该代码?我正在标题中实现Actor,所以我无法实现Image为什么不,Image
也是一个Actor
,具有TextureRegion
?但是我如何绘制它?我可以将标题设置为Image并将该Actor添加到stage吗?是的,如果您实现了Actor,那么您需要自己编写绘图部分,但是如果您的Headline
是Image的子级,那么您只需要在构造函数中传递drawable。公共类Headline扩展Image实现drawable,在menuscreen Image Image=new Image(new Headline(atlas.findRegion(Constants.MENUHEADLINE_REGION_name));图像。设置位置(100100);//位置适合所有设备屏幕。舞台演员(形象);