#Bevy Tween: Animation completed event is getting fired multiple times

52 messages · Page 1 of 1 (latest)

steady wedge
#

Logs:

on_release
Event: Release animation completed
on_release
Event: Release animation completed
Event: Release animation completed

Reproducable example:

use bevy::{color::palettes::css::*, prelude::*};
use bevy_tween::{
    combinator::{event, sequence, tween},
    interpolate::background_color_to,
    prelude::*,
};

#[derive(Component)]
struct Bulb;
fn main() {
    App::new()
        .add_plugins((DefaultPlugins, DefaultTweenPlugins))
        .add_systems(Startup, setup)
        .add_systems(Update, read_events)
        .add_observer(on_release)
        .run();
}
fn read_events(mut event_reader: EventReader<TweenEvent<&'static str>>) {
    event_reader.read().for_each(|event| {
        println!("Event: {}", event.data);
    });
}

fn on_release(
    mut trigger: Trigger<Pointer<Released>>,
    mut q_bulb: Query<Entity, With<Bulb>>,
    mut commands: Commands,
) {
    if let Ok(entity) = q_bulb.get_mut(trigger.target()) {
        trigger.propagate(false);
        println!("on_release");
        let mut entity_commands = commands.entity(entity);
        let node_target = entity_commands.id().into_target();
        let mut bg_color_state = node_target.state(WHITE.into());
        entity_commands.animation().insert(sequence((
            tween(
                Duration::from_secs(1),
                EaseKind::QuadraticOut,
                bg_color_state.with(background_color_to(RED.into())),
            ),
            event("Release animation completed"),
        )));
    }
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2d);
    commands.spawn((
        Node {
            width: Val::Percent(100.),
            height: Val::Percent(100.),
            ..default()
        },
        children![(
            Node {
                width: Val::Px(100.),
                height: Val::Px(100.),
                ..default()
            },
            Bulb,
            BackgroundColor(YELLOW.into()),
        )],
    ));
}

#

Expected Behavior
The "Release animation completed" event should be triggered once per pointer release.

Actual Behavior
The event is triggered multiple times for a single pointer release.

dense olive
steady wedge
#

i have opened one

#

@near thunder sorry for tagging
Please help if you have idea of above issue

near thunder
#

I also don't see github notifications if I'm not tagged there since I'm not the crate's creator

#

Alright, time to investigate
Would you rather I answer you here or on the github issue?

steady wedge
#

in most examples that i saw
on the animation end entities are removed
so it works fine
but my case
i want entity to be there
imagine kind of popup when user clicks a button
popup appears with animation
and when same button is clicked again
popup is closed with animation

near thunder
#

I do a lot of ui stuff with tweens in my cards crate if you want to have some examples of ways it can be used

steady wedge
near thunder
#

Just ran it, it logs more events the more times it's pressed

#

I'm trying to fix that for you as elegant as I can

steady wedge
#

correct

#

i think the issue is once the animation was completed first time
on second click
the first click tween components stuff is still there
not sure just a guess

#

possibly some cleaning is required, i don't know what all stuff to remove

near thunder
#

Give me a few minutes, ok?

steady wedge
#

yes sure

near thunder
#

What I did was:

  • Spawn a different animation entity with the ui entity as target
  • Register my plugins from bevy_tween_helpers (will send a link in a sec)
#

But
The code I wrote is still incomplete because it does not account for cases where you click it before the animation completed

#

So
What do you say- should I complete it, put it on gist and send it to you?

steady wedge
#

it would be great, i am new to bevy and animation
it will save my time

near thunder
#

It's ok

#

Feel free to have a look at the bevy_tween_helpers repo in the meanwhile or on bevy_play_card to see use examples
https://github.com/Rabbival/bevy_tween_helpers
https://github.com/Rabbival/bevy_play_card

GitHub

Optional, additional utilities for the bevy_tween crate - Rabbival/bevy_tween_helpers

GitHub

A card crate for the Bevy game engine. Contribute to Rabbival/bevy_play_card development by creating an account on GitHub.

steady wedge
#

i had also tried to use your bevy_tween_helpers
but i was not spawning different entity
so is it was despawning the box entity

near thunder
#

Haha so what you should have done is spawn the animation as a different entity and have that entity as a target

#

As I did here, will send the code in a bit

steady wedge
#

yes that was the missing part of puzzle😂

near thunder
#

I'm closing the github issue

#

Please mark this thread as solved when you have no more related issues/questions

steady wedge
#

Thank you🙂

near thunder
#

I'm not always as responsive haha but I try to be

near thunder
steady wedge
#

can we reverse a tween?

near thunder
#

Yes

#

Like, you can play it in reverse

steady wedge
#

so there is reverse method?

near thunder
steady wedge
#

ohh super

#

Thank you

#

closing thread

near thunder
#

You can also repeat it multiple times or infinitely and in different ways

#

It's a really cool crate haha
I'm not the guy who created it but I sure am grateful it exists

steady wedge
#

yes it is amazing
examples are awesome