libGdx - 缩放组不会影响孩子

时间:2015-03-10 21:12:29

标签: java libgdx actor

我有一个小组和演员。我向整个组添加移动和缩放操作。移动效果很好,但我有缩放问题 - 它不会影响孩子。我认为我的小组课中的绘画方法有问题,但我无法弄清楚是什么。

小组课程:

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

    }

}

演员班:

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

1 个答案:

答案 0 :(得分:2)

好的,我解决了这个问题,也许它会帮助别人。

在group类方法中,需要调用applyTransform()。现在缩放工作正常,但出现了一些移动问题 - 我认为原点需要修复。

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