简体   繁体   中英

How to resize a sprite from center in libgdx

Am gradualy increasing the size of a sprite then after it reaches a ceritain size it should gradualy reduce in size. The problem i have is that it's not resizing from the center.

private float size = 40;
private float v = 200;

In my update method i have

public void update(float delta){
    size += v * delta;

    if (size > 80){
        v = -200;
    }

    if (size <= 0) {
        size = 0;
    }

}

then i draw it

        sprite.setRegion(AssetLoader.atlas.findRegion("circle"));
        sprite.setPosition(centerX, centerY);
        sprite.setOriginCenter();
        sprite.setSize(size, size);
        sprite.draw(game.batch);

how do i make it resize from the center?

I'd assume that setOriginCenter sets the origin to the current center. So if you change the size, you'll need to call this method again. Did you consider changing the scale instead of the size?

ps: Spirte source link: https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/Sprite.java

setOriginCenter method set value of

originX = width / 2 
originY = height / 2;

so this method should be called after setting new size(width,height) of sprite.

It's better to use setScale method for your sprite.

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