如何在一个舞台上画出同一演员在不同位置的几个副本



我想在同一个舞台上画一个演员的几个副本。

这是我所做的:

//class Test

公共类Test扩展Image {

private Sprite sprite;
private Rectangle bounds;
private final float HEIGHT = Gdx.graphics.getHeight();
public Test() {
    // Sprite
    Texture texture = new Texture("img.png");
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    sprite = new Sprite(texture);
    // adjusting sprite size to different resolutions
    float imgHeightRatio = 120.0f / 480.0f;
    float imgWidthHeightRatio = sprite.getWidth() / sprite.getHeight();

    float newHeight = imgHeightRatio * HEIGHT;
    float newWidth = imgWidthHeightRatio * newHeight;
    clockSprite.setSize(newWidth, newHeight);
    // setting the size of the actor
    setSize(sprite.getWidth(), sprite.getHeight());

    // sprite bounds
    bounds = new Rectangle();
}
public void setBounds(Rectangle bounds) {
    this.bounds = bounds;
}
@Override
public void draw(Batch batch, float parentAlpha) {
// drawing sprite
    batch.draw(sprite, bounds.x, bounds.y, bounds.getWidth(), bounds.getHeight());
}

}//测试类结束

//class MyStage

公共类MyStage扩展Stage {

private Array<Test> actors;
private Rectangle rect1, rect2, rect3;
public MyStage(Test t) {
    // Rectangles
    rect1 = new Rectangle(0, 0, t.getWidth(), t.getHeight());
    rect2 = new Rectangle(30, 30, t.getWidth(), t.getHeight());
    rect3 = new Rectangle(60, 60, t.getWidth(), t.getHeight());
    actors = new Array<Test>();
    for(int i = 0; i < 3; i++) {
        actors.add(t);
    }
    for(Test test : actors) {
        addActor(test);
        test.setBounds(rect1);
        test.setBounds(rect2);
        test.setBounds(rect3);
    }
}

public void act(float dt) {
    super.act(dt);
}
@Override
public void draw() {
    super.draw();
}
public void dispose() {
    super.dispose();
}

}//MyStage类结束

您不能只是简单地复制Actor实例或将其不止一次添加到Stage,尽管您可以分配例如相同的纹理到所有它们(但我不确定它是否合理-可能是因为内存时有几十个相同的演员它是)。

你可以用非常简单的方法测试它:

Stage stage;
Actor a = new Actor();
stage.addActor(a);
System.out.println(stage.getActors().size);
stage.addActor(a);
System.out.println(stage.getActors().size); //same as above
在我看来,最好的想法是创建ActorFactory,对于不那么复杂的例子,比如你的方法返回Actor的新实例
public Test getTestActor(float x, float y, float width, float height)
{
    //here you are creating Test instance, giving it bounds etc
}

那么你可以像

那样处理它
stage.addActor ( getTestActor(0, 0, t.getWidth, t.getHeight)

还有一件事-如果添加演员副本到舞台是你扩展它的唯一原因,请不要。

首先,您的setBounds方法可能应该复制值而不是引用,因此您不会冒险将相同的Rectangle对象传递给多个不同的参与者。

public void setBounds(Rectangle bounds) {
    this.bounds.set(bounds);
}

在你的Test类中添加一个方法,允许你从原型中复制一个副本:

public Test (Test test){
    this();
    setBounds(test.bounds);
}

虽然在你的例子中,你给它们分配了不同的边界,所以这目前是不必要的。从类构造函数中加载纹理是错误的,因为这会导致类的每个实例都加载纹理的副本,而它们本可以共享一个副本。所以你的构造函数应该是这样的:

public Test(Texture texture) {
    sprite = new Sprite(texture);
    // adjusting sprite size to different resolutions
    float imgHeightRatio = 120.0f / 480.0f;
    float imgWidthHeightRatio = sprite.getWidth() / sprite.getHeight();
    float newHeight = imgHeightRatio * HEIGHT;
    float newWidth = imgWidthHeightRatio * newHeight;
    clockSprite.setSize(newWidth, newHeight);
    setSize(sprite.getWidth(), sprite.getHeight());
    // sprite bounds
    bounds = new Rectangle();
}
public Test (Test test){
    this();
    sprite.set(test.sprite);
    bounds.set(test.bounds);
}

现在复制这个是有意义的。设置你的测试用例:

public MyStage(Test prototypeTest) {
    int width = prototypeTest.getWidth();
    int height = prototypeTest.getHeight();
    rect1 = new Rectangle(0, 0, width, height);
    rect2 = new Rectangle(30, 30, width, height);
    rect3 = new Rectangle(60, 60, width, height);
    actors = new Array<Test>();
    for(int i = 0; i < 3; i++) {
        actors.add(new Test(prototypeTest));
    }
    for(Test test : actors) {
        addActor(test);
        test.setBounds(rect1);
        test.setBounds(rect2);
        test.setBounds(rect3);
    }
}

最新更新