多个动作添加到舞台=混乱

时间:2014-02-17 18:23:45

标签: action libgdx actor

我正在使用带有演员和舞台的scene2d。 在我的课程世界中,我需要在我的舞台上添加动作到IMAGES,例如:

Image impact;
Texture impactTexture = new Texture(Gdx.files.internal("data/impact.png"));
impact = new Image(impactTexture);
    impact.setPosition(x - impact.getWidth() / 2, y - impact.getHeight() / 2);      
    impact.setTouchable(Touchable.disabled);
    impact.addAction(sequence(fadeOut(0.2f), run(new Runnable() {
        @Override
        public void run() {
            impact.remove();
        }
    })));

    stage.addActor(impact);

此工作正常,图像显示并按需要淡出。 但是当我想向ACTOR添加相同的动作时,动作并不起作用。例如,我正在尝试将动作fadeOut添加到我的演员" Loot"。

Loot loot;  
loot = new Loot(new Vector2(x, y, 0, 0);
    loot.setTouchable(Touchable.disabled);
    Action lootAction = sequence(fadeOut(1f), moveTo(30,30,1), run(new Runnable() {
        @Override
        public void run() {
            loot.remove();
        }
    }));        
    loot.addAction(lootAction);     
    stage.addActor(loot);

我班上的代码Loot:

public class Loot extends Actor {

protected Vector2 position;
protected int width; 
protected int height;
protected Rectangle bounds;
protected float rotation;
protected int TYPE; 
protected int amount;
public Boolean movetoinventory = false;

Texture keyTexture = new Texture(Gdx.files.internal("data/key.png"));

public Loot(Vector2 position, float rotation, int TYPE) {

    this.position = position;
    this.width = keyTexture.getWidth();
    this.height = keyTexture.getHeight();
    bounds = new Rectangle(position.x, position.y, width, height);
    this.rotation = rotation;
    this.TYPE = TYPE;
}

public void draw(Batch batch, float alpha) {
    batch.draw(keyTexture, position.x, position.y, width/2, height/2, width, height, 1, 1, rotation, 0, 0, keyTexture.getWidth(), keyTexture.getHeight(), false, false);        
}

public void act(float delta){
super.act(delta);
}

为什么fadeOut操作适用于IMAGE而不是ACTOR?

我在一个单独的类WorldRenderer中渲染所有内容,其中包含:

public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

1 个答案:

答案 0 :(得分:3)

为什么Loot类中的act方法为空?

尝试添加super.act(delta);

顺便说一句,你不需要在你的班级中声明位置,宽度,高度......,演员已经有了这些字段。

动作动画演员属性,而不是你自己的属性。您需要修改代码以使用基类属性,如下所示:

public Loot(Vector2 position, float rotation, int TYPE) {

    // set position and size
    setBounds(position.x, position.y, keyTexture.getWidth(), keyTexture.getHeight());
    setRotation(rotation);
    this.TYPE = TYPE;
}

public void draw(Batch batch, float alpha) {
    batch.setColor(1f, 1f, 1f, alpha);
    batch.draw(keyTexture, getX(), getY(), getWidth()/2, getHeight()/2, getWidth(), getHeight(), 1, 1, rotation, 0, 0, keyTexture.getWidth(), keyTexture.getHeight(), false, false);        
}