【问题标题】:Fix messd up coordinates修复混乱的坐标
【发布时间】:2016-09-13 22:13:02
【问题描述】:

我目前正在编写一个必须避开小行星的游戏(图片)。不幸的是,我搞砸了我的坐标系。今天我想添加一些按钮,但是在设置坐标时,它更像是尝试坐标,而不是完全知道要做什么。 我为我的游戏设置了 WIDTH 和 HEIGHT 以及从 OrthographicCamera 继承的 OrthoCamera 类,并负责使游戏在每个设备上看起来都一样。

MyGdxGame 类:

public class MyGdxGame implements ApplicationListener {


SpriteBatch batch;
ShapeRenderer renderer;
OrthoCamera cam;
public static int WIDTH = 1080 , HEIGHT = 720; // "resolution"

OrthoCamera 类:

public class OrthoCamera extends OrthographicCamera {

Vector3 tmp = new Vector3();
Vector2 origin = new Vector2();
VirtualViewport virtualViewport;
Vector2 pos = new Vector2();

public OrthoCamera() {
    this(new VirtualViewport(MyGdxGame.WIDTH, MyGdxGame.HEIGHT));
}

我还让游戏屏幕“更大”,以便为飞行的小行星创造更多空间。

小行星类:

公共类 Asteroids 扩展实体 {

private final int anzahl = 150;
private float xMin, xMax, yMin, yMax;
private List<Asteroid> asteroids = new ArrayList<Asteroid>();

private final int zyklischeRandbedingungenVielfaches = 2;

public Asteroids() {

    xMin = MyGdxGame.WIDTH * (-zyklischeRandbedingungenVielfaches);
    xMax = MyGdxGame.WIDTH * (zyklischeRandbedingungenVielfaches);
    yMin = MyGdxGame.HEIGHT * (-zyklischeRandbedingungenVielfaches);
    yMax = MyGdxGame.HEIGHT * (zyklischeRandbedingungenVielfaches); 

    for (int i = 0; i < anzahl; i++) {
        Asteroid a = new Asteroid( xMax,  yMax, asteroids);
        asteroids.add(a);
    }

}

小行星类:

 public void update(float deltaT, float xMin, float xMax, float yMin, float yMax) {
    p.x += v.x * deltaT;
    p.y += v.y * deltaT;

    while(p.x > xMax)
    {
        p.x -=  (xMax - xMin);  
    }
    while(p.x < xMin)
    {
        p.x += (xMax - xMin);
    }
    while(p.y > yMax)
    {
        p.y -= (yMax - yMin); 
    }
    while(p.y < yMin)
    {
        p.y += (yMax - yMin); 
    }

例如,这就是我设置播放按钮位置的方式:

   texture = new Texture("button.png");

    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    TextureRegion region = new TextureRegion(texture, 59, 52, 300,250);

    sprite = new Sprite(region);
    sprite.setSize(sprite.getWidth() / 2, sprite.getHeight() / 2);
    sprite.setOrigin(-sprite.getWidth() / 2, -sprite.getHeight() / 2);

    sprite.setPosition(MyGdxGame.WIDTH / 2  - 317, MyGdxGame.HEIGHT /2 - 110); //!!!

希望有人能告诉我如何创建更简单的坐标。 -谢谢-

【问题讨论】:

    标签: java android libgdx coordinates


    【解决方案1】:

    阅读文档here。具体来说:

    Sprite 总是矩形的,它的位置 (x, y) 位于 该矩形的左下角。

    因此,如果您想要将精灵置于中心的坐标,则需要确保从该坐标中减去精灵的一半宽度和一半高度。

    如果您需要更多详细信息,请参阅this question 接受的答案。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多