精灵不在位置上 [英] Sprite not on position
本文介绍了精灵不在位置上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的子画面不在不同的屏幕尺寸上.我该怎么办?
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.
推荐答案
使用视口满足您的要求,我认为APP_WIDTH
和APP_HEIGHT
是恒定的.
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是具有TextureRegion
的Actor
,为什么不使用Image
满足您的要求,如果您想要一些额外的属性,请使用Image
类扩展HeadLine.
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);
编辑
public class Headline extends Image {
public Headline(TextureRegion textureRegion) {
super(textureRegion);
}
}
并且在MenuScreen
stage.addActor(new Headline(atlas.findRegion(Constants.MENUHEADLINE_REGION_NAMES)));
这篇关于精灵不在位置上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文