如何在libgdx中命名一个Actor?

时间:2012-04-22 11:37:03

标签: java libgdx

演员下面没有名字。在libgdx中,最好的方法是为每个actor分配一个名字吗?

public class JewelsMainActivity implements ApplicationListener, InputProcessor {

private Vector2 point = new Vector2();
public TextureAtlas jewelsAtlas;
public Texture jewels;
private SpriteBatch batch;  
public Actor[][] actors = new Actor[10][10];
public Stage stage;
public int gridSize;

@Override
public void create() {
    stage = new Stage(800, 600, false);
    gridSize = 10;
    batch = new SpriteBatch();
    jewels = new Texture(Gdx.files.internal("assets/gems1.png"));
    jewelsAtlas = new TextureAtlas(Gdx.files.internal("assets/pack"));
    for (int z = 0; z < gridSize; z++) {
        for (int x = 0; x < gridSize; x++) {
                Random r = new Random();
                int j = r.nextInt(2)+1;
                actors[x][z] = new Image(jewelsAtlas.findRegion(""+j+""));
                actors[x][z].x = x*56; //each gem is 56*56 width / height
                actors[x][z].y = z*56;

// actors [x] [z] .name =“actor”+ x + z; //不能使用上面的内容,因为它不允许......

                stage.addActor(actors[x][z]);

        }
    }
    Gdx.input.setInputProcessor(this);
}


@Override
public void render() {
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
      batch.begin();
      stage.draw();
      batch.end();
}

@Override
public boolean touchDown(int x, int y, int pointer, int button) {
       stage.toStageCoordinates(Gdx.input.getX(), Gdx.input.getY(), point);
       Actor actor = stage.hit(point.x, point.y);
        if (actor != null){

            Gdx.app.log("name", actor.name);

// actor.name为空

            Gdx.app.log("hashCode", ""+actor.hashCode());
            ((Image)actor).color.set((float)Math.random(), (float)Math.random(), (float)Math.random(), 0.5f + 0.5f * (float)Math.random());
        }
    return false;
}

.... .... ....

}

2 个答案:

答案 0 :(得分:2)

名称是final,因此必须在构造函数中定义。

你在哪里

actors[x][z] = new Image(jewelsAtlas.findRegion(""+j+""));

使用

actors[x][z] = new Image(jewelsAtlas.findRegion(""+j+""), Scaling.none, Align.CENTER, "myawesomenewname");

答案 1 :(得分:0)

Actor.setUserObject(Object obj);可能也会派上用场。

e.g。演员[X] [Z] .setUserObject(&#34; nameHere&#34);

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/Actor.html#setUserObject-java.lang.Object-