【问题标题】:libgdx get screen width and height for 2d gameslibgdx 获取 2d 游戏的屏幕宽度和高度
【发布时间】:2014-03-10 22:35:50
【问题描述】:

我刚刚开始开发安卓游戏。我正在尝试为我的精灵设置动画,并且我已经成功地做到了,但是现在我通过按钮进行绘制,以便我可以开始让我的玩家移动,但他们不在应该在的正确位置。谁能帮助我? 它在桌面版上看起来很棒,但在我的安卓手机上却不行。

这是我的主类代码。

package com.Adventcloud.RealGame;

public class RealGame implements ApplicationListener {
  private OrthographicCamera camera;
private SpriteBatch batch;
//private Texture background;
private AnimatedSprite playerAnimated;

//Buttons
public static Texture leftButton;
public static Texture rightButton;
public static Texture shootButton;
public static Texture jumpButton;


@Override
public void create() {      
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    Texture.setEnforcePotImages(false);

    camera = new OrthographicCamera();
    camera.setToOrtho(false, 1280, 720);

    leftButton = new Texture(Gdx.files.internal("leftButton.png")); 
    rightButton = new Texture(Gdx.files.internal("rightButton.png"));
    shootButton = new Texture(Gdx.files.internal("shootButton.png"));   
    jumpButton = new Texture(Gdx.files.internal("jumpButton.png"));

    batch = new SpriteBatch();

    //background = new Texture(Gdx.files.internal(""));
    Texture spaceshipTexture = new Texture(Gdx.files.internal("spritesheet.png"));
    Sprite playerSprite = new Sprite(spaceshipTexture);
    playerAnimated = new AnimatedSprite(playerSprite);
    playerAnimated.setPosition(1280/2, 0);
}

@Override
public void dispose() {
    batch.dispose();
}

@Override
public void render() {      
    Gdx.gl.glClearColor(0.95f, 0.95f, 0.95f, 0.95f);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
        //batch.draw(background, 0, 0);
        playerAnimated.draw(batch);
        batch.draw(leftButton, 0, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
        batch.draw(rightButton, Gdx.graphics.getWidth()/4, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
        batch.draw(shootButton, (Gdx.graphics.getWidth()/4)*2, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
        batch.draw(jumpButton, (Gdx.graphics.getWidth()/4)*3, 0, Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/6);
    batch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
}

这是我的动画精灵代码

package com.Adventcloud.RealGame;

public class AnimatedSprite {
private static final int FRAME_COLS = 16;
private static final int FRAME_ROWS = 16;

private static final int IDLE_COLS = 4;
private static final int IDLE_ROWS = 1;

private Sprite sprite;
private Animation animation;
private TextureRegion[] frames;
private TextureRegion currentFrame;

private float stateTime;

public AnimatedSprite(Sprite sprite){
    this.sprite = sprite;
    Texture texture = sprite.getTexture();
    TextureRegion[][] temp = TextureRegion.split(texture, texture.getWidth() / FRAME_COLS, texture.getHeight() / FRAME_ROWS);
    frames = new TextureRegion[IDLE_COLS * IDLE_ROWS];
    //Animation for idling ======================================
    int index = 0;
    for (int i = 0; i < IDLE_ROWS; i++) {
        for (int j = 1; j <= IDLE_COLS; j++) {
            frames[index++] = temp[i][j];
        }
    }
    animation = new Animation(0.2f, frames);
    stateTime = 0f;
    //end of idle animation ====================================
}

public void draw(SpriteBatch spriteBatch) {
    stateTime += Gdx.graphics.getDeltaTime();
    //continue to keep looping
    currentFrame = animation.getKeyFrame(stateTime, true);

    spriteBatch.draw(currentFrame,  sprite.getX() - 32, sprite.getY() + 128, 128, 128);
}

public void setPosition(float x, float y){
    float widthOffset = sprite.getWidth() / FRAME_COLS;
    sprite.setPosition(x- widthOffset / 2, y);
}
}

【问题讨论】:

    标签: libgdx


    【解决方案1】:

    这实际上是相机的用途(至少是其中一件事)。您可以使用自定义视口,它将按比例放大以适合屏幕/窗口。 例如,如果您想要 16/9 的纵横比,并且您的屏幕应该是 80“世界单位”(也许是米?)宽,那么使用 80 宽和 45 高的视口。 所以使用camera = new OrthographicCamera(80, 45); 这将创建一个宽度为 80 个单位、高度为 45 个单位、中间为 P(0,0) 的相机。 通过这样做,Texture(大小为1)在1600*900px 屏幕上为20px 宽和20px 高,在800*450px 屏幕上为10px 宽和10px 高。 在坐标 P(0,0) 处绘制对象会在屏幕中间绘制其左下角,在 P(-40, 0) 处绘制对象会将其绘制在左边框。 不要忘记告诉SpriteBatch 使用这台相机:

    spriteBatch.setProjectionMatrix(camera.combined);
    

    要移动相机,请使用camera.translate(deltaX, deltaY); 如果您移动相机或旋转它或做其他事情,它的矩阵发生了什么变化不要忘记调用camera.update()

    现在您可以计算“worldunits”中的所有内容,并将按比例放大以适应屏幕。 希望能帮到你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 2010-11-25
      • 2011-09-25
      • 2011-06-12
      • 2012-11-01
      • 2021-02-07
      • 1970-01-01
      相关资源
      最近更新 更多