简体   繁体   English

将精灵与libGDX叠加

[英]Superposed the sprites with libGDX

I would like to superpose two sprites but, when I resize my windows the sprites moves ... 我想叠加两个精灵,但是,当我调整窗口大小时,精灵会移动...

My code: 我的代码:

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

Sprite#setOrigin Sets the origin in relation to the sprite's position for scaling and rotation. Sprite#setOrigin设置相对于精灵位置的原点,以进行缩放和旋转。 In this code you set the same origin for both sprites despite one of them being considerably smaller: 在此代码中,您为两个精灵设置了相同的原点,尽管其中一个非常小:

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

Change the billeBlanche origin to this: 将billeBlanche来源更改为此:

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

Also,in resize the position you set for the sprites is fixed -always (width-700)/2 or (width-400)/2- but your sprite sizes is different. 同样,在调整大小时 ,您为子画面设置的位置始终是固定的(width-700)/ 2或(width-400)/ 2-,但是您的子画面大小不同。 Thats the reason they "move" . 这就是他们“行动”的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM