如何在一个舞台上的不同位置绘制同一演员的多份副本? [英] How to draw several copies of the same actor on one stage at different positions?
问题描述
我想在同一舞台上绘制一个演员的多个副本.
I want to draw several copies of an actor on the same stage.
这是我所做的:
//类测试
公共类Test扩展了图像{
public class Test extends 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
// class MyStage
公共类MyStage扩展了阶段{
public class MyStage extends 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类的结尾
} // end of MyStage class
推荐答案
您不能只是简单地复制 Actor 实例或将其多次添加到 Stage ,尽管您可以可以为所有这些对象分配相同的纹理(但我不确定这是否合理-可能是因为拥有多个相同演员时的记忆力).
you cannot just simply duplicate Actor instance or add it more than once to the Stage, although you can assign for example the same Texture to all of them (but I'm not sure if it is reasonable - maybe because of memory when having dozens of same actor it is).
您可以通过非常简单的方式对其进行测试:
You can test it in very simple way:
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的新实例的方法
The best idea in my opinion is to create ActorFactory and for less complicated examples like your just the method returning new instance of the Actor
public Test getTestActor(float x, float y, float width, float height)
{
//here you are creating Test instance, giving it bounds etc
}
那你就可以像这样处理它
Then you can just deal with it like
stage.addActor ( getTestActor(0, 0, t.getWidth, t.getHeight)
还有一件事-如果将演员副本添加到舞台是您扩展舞台的唯一原因,请不要这样做.它将使您的应用程序将来的灵活性大大降低.
One thing more - if adding actor copies to the stage is the only reason you are extending it please don't. It will make your application far less flexible in future.
这篇关于如何在一个舞台上的不同位置绘制同一演员的多份副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!