简体   繁体   中英

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. 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.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. Thats the reason they "move" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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