Question

I am using scene2d with actors and a stage. In my class World I need to add actions to IMAGES to my stage, for example:

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);

This works fine, the image is displayed and fades out as wanted. But when I want to add the same action to an ACTOR, the action doesn't work. For example, I am trying to add the action fadeOut to my actor "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);

And the code of my class 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);
}

Why is the fadeOut action working on an IMAGE and not an ACTOR?

I render everything in a separate class WorldRenderer which has:

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();
}
Was it helpful?

Solution

Why the act method in your Loot class is empty ?

Try adding super.act(delta);.

By the way, you don't need to declare position, width, height... in your class, Actor has already theses fields.

Actions animates actors properties, not your own. You need to modify your code to use the base class properties, like this :

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);        
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top