Search code examples
javalibgdxscene2d

Changing properties of child objects(called at the same time) in a sequence action after one of actions has been completed in LibGDX


The idea of the system is when a user clicks a texture it will move to the icon which is an ImageButton.

I have a method transitionTextureToActor(). If a user clicks the texture this method is called.

This method just changes properties of the Image so this Image will duplicate a texture and it moves to the icon which is ImageButton. After action of movement and sizing is completed the Image is supposed to become invisible.

private void transitionTextureToActor(ShapeProcessor shapeProcessor, Image icon) {
    tempActor = shapeProcessor.getAnimatedImage();
    tempActor.setSize(shapeProcessor.getWidth(), shapeProcessor.getHeight());
    tempActor.setPosition(shapeProcessor.getX(), shapeProcessor.getY());
    tempActor.setVisible(true);
    shapeProcessor.setVisible(false);

    tempActor.addAction(
            sequence(
                    parallel(
                            sizeTo(icon.getDrawable().getMinWidth(), icon.getDrawable().getMinHeight(), .3F),
                            moveTo(icon.getX(), icon.getY(), .3f)
                    ),
                    run(new Runnable() {
                        @Override
                        public void run() {
                            tempActor.setVisible(false);
                        }
                    })
            )
    );
}

So it works well for a single call of transitionTextureToActor method. After resizing and movement the image disappears.

But when I call several objects of ShapeProcessor at the same time only first one disappears.

  for(ShapeProcessor shape: shapes){
        transitionTextureToActor(shape);
    }

I want all of them to be invisible. Must be something wrong with RunnableAction because there the image becomes invisible but I can't work out what.


Solution

  • That's because at the time, run() methods of RunnableActions are called, they all point at the same object, referenced by tempActor. Try creating local references and pass them to RunnableActions:

    private void transitionTextureToActor(ShapeProcessor shapeProcessor, Image iconClicked) {
        tempActor = shapeProcessor.getAnimatedImage();
        tempActor.setSize(shapeProcessor.getWidth(), shapeProcessor.getHeight());
        tempActor.setPosition(shapeProcessor.getX(), shapeProcessor.getY());
        tempActor.setVisible(true);
        shapeProcessor.setVisible(false);
    
        final Actor localReference = tempActor;
    
        tempActor.addAction(
                sequence(
                        parallel(
                                sizeTo(iconClicked.getDrawable().getMinWidth(), iconClicked.getDrawable().getMinHeight(), .3F),
                                moveTo(iconClicked.getX(), iconClicked.getY(), .3f)
                        ),
                        run(new Runnable() {
                            @Override
                            public void run() {
                                localReference.setVisible(false);
                            }
                        })
                )
        );
    }