用libGDX叠加精灵

时间:2013-10-30 10:45:17

标签: java resize libgdx sprite

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

我的代码:

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

1 个答案:

答案 0 :(得分: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);

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