Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Guest

Method onComplete - few functions

Go to solution Solved by OSUblake,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

I am new to the study of GSAP. In the study a ready example of a HTML5-game ( GSAP ) I have a question.

 

The method onComplete is passed to one callback function:



...
onComplete: oneFunction


 

Is it possible onComplete method to pass multiple callback functions?

 



...
onComplete: firstFunction secondFunction


 

For example, I have a task to hang on one event call multiple functions.

 

Or is this way impossible and the only solution in this case is to call the callback functions in the chain - at the conclusion of one function this function calls another function and so on?

Link to comment
Share on other sites

  • Solution

There can only be 1 function per callback type. What you're describing would be an event emitter/dispatcher. Using an event emitter is a common approach to game design, and it's something I wondered about when I first started using GSAP because I noticed in the source code that GSAP uses an event emitter to drive the callbacks.

 

There are ways to tap into GSAP's event emitter or intercept the callbacks, but that's not something I would recommend doing. If you want to use events, you could set a callback to emit an event from your own emitter. Here's a demo I made using that approach. Instead of running the logic to figure when a leaf should grow in a callback, I just emit an event and let the leaves figure that out. When a leaf starts growing, it stops listening for that event.

 

See the Pen gpGEye?editors=0010 by osublake (@osublake) on CodePen

 

That demo might be a little hard to understand, so here's a really simple example using EventEmitter3. You will see listenerA gets called when the tween first starts, listenerB gets called on the first repeat, and listenerC gets called 2 times on every repeat.

 

See the Pen 4bc0ee5b3e3f3e41647140e836619439?editors=0011 by osublake (@osublake) on CodePen

 

.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×