[Solved(-ish)] How to remove an effect

Hello!

So I’m trying to apply a “cooldown” effect to my buttons such that after a skill is fired, the button should turn black and a timer appears over it. I can get the button to turn black but I can’t seem to remove the black effect and thus see the original icon again after the timer runs out.

Because it is possible to remotely fire skills via keyboard shortcuts, I don’t feel like having the button subscribe to a Release EffectEvent is what I need.

Right now my code looks something like this…

private class buttonCooldown extends AbstractControl {
    private float countdown;
    private MyButton button;
    private Effect buttondim;

    public buttonCooldown(int length, MyButton button) {  //no important difference from a normal button
        this.countdown = length;
        this.button = button;
        button.setFontColor(ColorRGBA.White);
        button.setFontSize(36);
        buttondim = new Effect(Effect.EffectType.ColorSwap, Effect.EffectEvent.Show, countdown);
        buttondim.setColor(ColorRGBA.Black);
        buttondim.setElement(button);
        screen.getEffectManager().applyEffect(buttondim);
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (countdown > 0) {
            countdown -= tpf;
            button.setText(new Integer((int) countdown + 1).toString());
        } else {
            button.setText("");
           //screen.getEffectManager().removeEffect(buttondim) something like that?
            spatial.removeControl(this);
        }
A separate issue but may be important: using this.button.addEffect(buttondim); actually doesn't turn the button black at all, although some other effects work.

Thanks in advance!

If am not wrong this is what you are looking for :

button.removeEffect(Effect.EffectEvent.ColorSwap);

But am not totaly sure. If this don’t work tell me, il check it deeper later.

Thanks for you response,

Effect.EffectEvent only contains the activating event (like Show, OnFocus, Hover, etc). I’ve used the complimentary button.removeEffect(Effect.EffectEvent.Show); but that doesn’t work, either by adding the effect to the button or to EffectManager.

Try just creating a new button (just an idea i got).

Actually, replacing the entire button does work. I feel like this is the long way around and it definitely took a lot of debugging to get it functional, but…

protected void controlUpdate(float tpf) {
            if (countdown > 0) {
                countdown -= tpf;
                button.setText(new Integer((int) countdown + 1).toString());
            } else {

                String name = button.getName().split(":")[0];

                MyButton replacebutton = new MyButton(screen, name,
                        button.getPosition(), button.getDimensions(),
                        new Vector4f(1, 1, 1, 1), button.getElementTexture().getName()) {
                    @Override
                    public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
                        inputSystem.manualFire(name.split(":")[0]);
                    }
                };
                buttons.put(name, replacebutton);
                HUDNode.removeChild(HUDNode.getChildElementById(name));
                HUDNode.addChild(replacebutton);
                spatial.removeControl(this);
            }

If anyone else comes up with a more concise solution, I’d love to hear it. Thanks for your suggestion!

You’re welcome ;)!

This may or may not help you, depending on how your UI and the rest of your game is set up, but my skill icons use a shader to change their overlay. It’s relatively easy to code, just make a greyscale mask in gimp in a spiral shape, then have the shader darken pixels that are darker than the percent on the timer (or if you’re displaying the timer as text, you don’t even need the mask!)

1 Like