Search code examples
javalibgdxactor

libGdx - Scaling group doesn't affect children


I have a group and actors inside of it. I add moving and scaling actions to whole group. Moving works great but I have a problem with scaling - it doesnt affect children. I think there is something wrong with drawing method in my group class but I cant figure out what.

Group class:

public class LevelDetails extends Group{

    float x;
    float y;

    TextureRegion texture;
    Button button;
    Button imgPrev;
    Button stageName;

    public LevelDetails(float x, float y, int state, TextureRegion texture){
        this.x = x;
        this.y = y;

        float textureWidth = texture.getRegionWidth();
        float textureHeight = texture.getRegionHeight();

        this.texture = texture;

        this.setBounds(x, y, textureWidth, textureHeight);
        this.setPosition(x, y);

        this.addAction(Actions.scaleBy(.75f, .75f, .5f,Interpolation.fade));


        button = new Button(Assets.buttonTextReg, (int)(this.getX()+this.getWidth()/2-Assets.buttonTextReg.getRegionWidth()/2), 50, Assets.buttonTextReg.getRegionWidth(), Assets.buttonTextReg.getRegionHeight());
        button.setBounds((int)(this.getX()+this.getWidth()/2-Assets.buttonTextReg.getRegionWidth()/2), 50, Assets.buttonTextReg.getRegionWidth(), Assets.buttonTextReg.getRegionHeight());
        this.addActor(button);      

    }

    @Override
    public void draw(Batch batch, float parentAlpha){

        batch.draw(texture, this.getX(), this.getY(), this.getOriginX(),this.getOriginY(), texture.getRegionWidth(), texture.getRegionHeight(),
                this.getScaleX(), this.getScaleY(),this.getRotation()); 
        drawChildren(batch, parentAlpha);

    }

}

Actor class:

public class Button extends Actor{

    TextureRegion texture;
    int x;
    int y;

    public Button (TextureRegion texture, int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.texture = texture;
        this.setPosition(x, y);
        this.setBounds(this.getX(), this.getY(), texture.getRegionWidth(), texture.getRegionHeight());
    }

    @Override
    public void draw(Batch batch, float parentAlpha){
        batch.draw(texture, this.getX(), this.getY()+this.getParent().getY(), texture.getRegionWidth(), texture.getRegionHeight());
    }
}

Solution

  • Ok, I solved that problem, maybe it'll help someone.

    In group class method applyTransform() needs to be called. Now scaling works fine but some moving issues appeared – origins need to be fixed I think.

    @Override
    public void draw(Batch batch, float parentAlpha){      
      batch.draw(texture, this.getX(), this.getY(), this.getOriginX(),this.getOriginY(), texture.getRegionWidth(), texture.getRegionHeight(), this.getScaleX(), this.getScaleY(),this.getRotation());
    
      applyTransform(batch, computeTransform());
    
      drawChildren(batch, parentAlpha);
    
      resetTransform(batch);
    }