1

我想叠加两个精灵但是,当我调整我的窗口大小时,精灵会移动......

我的代码:

public void resize(int width, int height) {
    // TODO Auto-generated method stub
    if ( height < width ){
        spriteAbalone.setScale(height/700f);
        billeBlanche.setScale(height/700f);
    }
    else{
        spriteAbalone.setScale(width/700f);
        billeBlanche.setScale(width/700f);
    }
    spriteAbalone.setPosition((width-700)/2, (height - 700)/2);
    billeBlanche.setPosition((width-400)/2,(height-400)/2);
    camera.setToOrtho(false, width, height);
    camera.update();
}


@Override
public void show() {
    // TODO Auto-generated method stub
    float screenW = Gdx.graphics.getWidth();
    float screenH = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, screenW, screenH);
    batch = new SpriteBatch();  
    spriteAbalone = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/AbaloneCS5.gif")), 0, 0, 704, 704));
    spriteAbalone.setSize(700 , 700);
    spriteAbalone.setOrigin(704/2,704/2);
    billeBlanche = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/billeblanche.gif")),0,0,200,200));
    billeBlanche.setSize(65, 65);
    billeBlanche.setOrigin(704/2,704/2);
}
4

1 回答 1

1

Sprite#setOrigin设置相对于精灵位置的原点以进行缩放和旋转。在这段代码中,您为两个精灵设置了相同的原点,尽管其中一个要小得多:

spriteAbalone = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/AbaloneCS5.gif")), 0, 0, 704, 704));
spriteAbalone.setSize(700 , 700);
spriteAbalone.setOrigin(704/2,704/2);
billeBlanche = new Sprite(new TextureRegion(new Texture(Gdx.files.internal("data/billeblanche.gif")),0,0,200,200));
billeBlanche.setSize(65, 65);
billeBlanche.setOrigin(704/2,704/2);

将 billeBlanche 原点更改为:

billeBlanche.setOrigin(200/2,200/2);

此外,在调整大小时,您为精灵设置的位置是固定的 - 总是 (width-700)/2 或 (width-400)/2- 但您的精灵大小不同。这就是他们“移动”的原因。

于 2014-01-08T22:28:03.643 回答