I have an image-button and I set its position in create in this way:


The problem is that the position always changes, despite I set it in create. Also if I change device, I notice that the coordinates are busted, wrong, instead other views are right.

If I write setPositionin render, the image-button is faster than the camera, it gets away from the screen, but for other views, this doesn't happen because they follow the camera. Why?

  • \$\begingroup\$ Do you want it to follow the camera or be static? \$\endgroup\$ – Charanor Jun 14 '17 at 10:51
  • \$\begingroup\$ @Charanor follow the camera \$\endgroup\$ – Curio Jun 14 '17 at 10:54

There are several possible solutions to this.

Try using stage coordinates instead like so: button.setPosition(150, 150). Stage always start at (0, 0) bottom left corner.

Use 2 separate cameras for your UI and your game.

OrthographicCamera gameCam = ...;
OrthographicCamera stageCam = ...;
Viewport viewport = new FitViewport(1280, 720, stageCam); //Constant viewport size
Stage stage = new Stage(viewport);

Then use gameCam for all of your game related stuff and use stageCam for your UI. I use a FitViewport here for our stage's viewport since it preserves aspect ratio. Makse sure to read up on Viewports to more clearly understand what viewport fits your need.

| improve this answer | |
  • \$\begingroup\$ getX()? There isn't getX(); \$\endgroup\$ – Curio Jun 14 '17 at 11:28
  • \$\begingroup\$ @Curio my bad, it should just be 150. \$\endgroup\$ – Charanor Jun 14 '17 at 12:12
  • \$\begingroup\$ If I change the device, it changes the position, like before \$\endgroup\$ – Curio Jun 14 '17 at 13:40
  • \$\begingroup\$ @Curio could you provide some screenshots? This is probably because the viewport sizes are different but I need to see to be sure. I've also updated my answer with a potential solution (see stageCam). \$\endgroup\$ – Charanor Jun 14 '17 at 13:44
  • \$\begingroup\$ I don't know if I can take some screenshots at the moment, but I can tell you that the image-button changes position but other views not. \$\endgroup\$ – Curio Jun 14 '17 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.