简体   繁体   中英

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.

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

EDIT

public class Headline extends Image {

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

And In setHeadline() method of MenuScreen

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM