我想在同一个舞台上画一个演员的几个副本。
这就是我所做的:
// class Test
public class 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扩展阶段{
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类的结尾
答案 0 :(得分:1)
您不能简单地复制 Actor 实例或多次将其添加到舞台,尽管您可以指定相同的纹理对所有人来说(但我不确定它是否合理 - 可能是因为记忆时有几十个相同的演员)。
您可以用非常简单的方式测试它:
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)
还有一件事 - 如果将演员副本添加到舞台是你扩展它的唯一原因,请不要这样做。它将使您的应用程序在未来变得不那么灵活。
答案 1 :(得分:0)
首先,您的public void setBounds(Rectangle bounds) {
this.bounds.set(bounds);
}
方法应该复制值而不是引用,因此您不会冒险将同一个Rectangle对象传递给多个不同的actor。
public Test (Test test){
this();
setBounds(test.bounds);
}
向Test类添加一个方法,允许您从原型中复制:
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);
}
}
现在制作副本是有意义的。所以要设置你的测试用例:
<a href="#" class="ctaBlock" name="">text text text</a>