Search code examples
libgdximage-resizing

Libgdx ScaleToAction not resizing


I am trying to make a sprite move, rotate and resize by using MoveToAction, RotateToAction and ScaleToAction. The first two works fine, but I have a problem with ScaleToAction. I add the action to the Actor just like I do with the two that works. I think the problem might be in the @Override? When I run the code the sprite moves and rotates but no scaling is done.

I tried to use sprite.setscale as suggested in the answer below, but still no luck. I add the code from the class here:

public class Prizes extends Actor {
private LearnToRead game;
private TextureAtlas atlas;
private TextureRegion prizepic;
private Sprite sprite;
private RotateToAction rta;
private ScaleToAction sta;
private MoveToAction mta;

public Prizes(LearnToRead game) {
    this.game = game;
    atlas = new TextureAtlas("prizes.pack");
    prizepic = atlas.findRegion("haxhatt");

    sprite = new Sprite(prizepic);
    sprite.setPosition(450 - sprite.getWidth() / 2, 450 * Gdx.graphics.getHeight() / Gdx.graphics.getWidth() - sprite.getHeight() / 2);
    //setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());

    setTouchable(Touchable.enabled);

    rta = new RotateToAction();
    sta = new ScaleToAction();
    mta = new MoveToAction();

    rta.setRotation(180f);
    sta.setScale(2f);
    mta.setPosition(0, 0);
    mta.setDuration(5f);
    rta.setDuration(5f);
    sta.setDuration(5f);
    Prizes.this.addAction(rta);
    Prizes.this.addAction(sta);
    Prizes.this.addAction(mta);

}

@Override
public void draw(Batch batch, float parentAlpha) {
    sprite.draw(batch);
}

@Override
public void act(float delta) {
    super.act(delta);
}

@Override
protected void positionChanged() {
    sprite.setPosition(getX(), getY());
}

@Override
protected void rotationChanged() {
    sprite.setRotation(getRotation());
}

@Override
protected void sizeChanged() {
    sprite.setScale(getScaleX(), getScaleY());
}

}

If also tried to remove sprite and just use the TextureRegion, but didn't get it to work. The texture is drawn, but not moving. I post that code as well, but I do confess that I am quite uncertain about this code:

public class Prizes extends Actor {
private LearnToRead game;
private TextureAtlas atlas;
private TextureRegion prizepic;
private RotateToAction rta;
private ScaleToAction sta;
private MoveToAction mta;

public Prizes(LearnToRead game) {
    this.game = game;
    atlas = new TextureAtlas("prizes.pack");
    prizepic = atlas.findRegion("haxhatt");

    Prizes.this.setBounds(450 - Prizes.this.getX(), Prizes.this.getY(), Prizes.this.getWidth(), Prizes.this.getHeight());

    setTouchable(Touchable.enabled);

    rta = new RotateToAction();
    sta = new ScaleToAction();
    mta = new MoveToAction();

    rta.setRotation(180f);
    sta.setScale(2f);
    mta.setPosition(0, 0);
    mta.setDuration(5f);
    rta.setDuration(5f);
    sta.setDuration(5f);
    Prizes.this.addAction(rta);
    Prizes.this.addAction(sta);
    Prizes.this.addAction(mta);

}

@Override
public void draw(Batch batch, float parentAlpha) {
    game.batch.begin();
    game.batch.draw(prizepic, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
    game.batch.end();
}

@Override
public void act(float delta) {
    super.act(delta);
}

@Override
protected void positionChanged() {
    Prizes.this.setPosition(getX(), getY());
}

@Override
protected void rotationChanged() {
    Prizes.this.setRotation(getRotation());
}

@Override
protected void sizeChanged() {
    Prizes.this.setScale(getScaleX(), getScaleY());
}

}

Maybe someone has a good idea about what I am doing wrong?


Solution

  • Use sprite.setScale instead of sprite.scale. The difference is that you are setting it a specific value instead of multiplying the current scale by some value.

    But it is redundant to use Sprite with Actor because both classes store position, rotation, scale, and color. It makes more sense to use a TextureRegion with Actor. Or you can use the Image class, which already handles this for you.

    Edit:

    I see the other part of the issue. You are overriding sizeChanged, but it's the scale, not the size, that you are changing with a ScaleToAction. Actor doesn't have a callback for the scale changing. You could override the setScale methods to apply your change to the Sprite, but like I said above, it doesn't make sense to be using a Sprite for this. You should reference a TextureRegion, and draw it with all the appropriate parameters in the draw() method.