【问题标题】:Sprite not on position精灵不在位置
【发布时间】:2017-05-19 00:56:09
【问题描述】:

我的精灵不在不同的屏幕尺寸上。我该怎么办?

我的头条演员长这样:

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

我在我的屏幕类中调用。

【问题讨论】:

  • 如何创建stage,显示该代码?

标签: java android libgdx


【解决方案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。

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

而在MenuScreensetHeadline()方法中

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

【讨论】:

  • 我在 Headline 中实现 Actor,所以我无法实现 Image
  • 为什么不呢,Image 也是一个Actor,拥有TextureRegion
  • 但是我怎么画它,我可以将标题设置为图像并将这个演员添加到舞台上吗?
  • 是的,如果你实现了Actor那么你需要自己写draw部分但是如果你的Headline是Image的孩子那么你只需要在构造函数中传递drawable。
  • public class Headline extends Image 实现 Drawable 并在 menuscreen Image image=new Image(new Headline(atlas.findRegion(Constants.MENUHEADLINE_REGION_NAMES))); image.setPosition(100,100); // 位置适合所有设备屏幕。 stage.addActor(image);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多