Jump to content
GreenSock

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

getParent method?

Recommended Posts

I know there's a getChildren() method for TimelineMax but I have a situation where I need to check to see if a child timeline has been added to a parent timeline. Would be great if I could do something like getParent() and then check against what I know to be the master timeline - as opposed to getting the children of a master timeline and having to look through it to see if the child is there.

 

I know that every timeline created is added to a global timeline until it gets added to a specific parent timeline so the .timeline property is never empty. 

 

Would like to do something like:

var tw,
    tl = new TimelineMax(),
    masterTL = new TimelineMax();

function doTween() {
  resetTween(tw); // reset tween function
  tw = TweenMax.to(element, 1, {x: 0, y:0});
  tl.add(tw);
  var master = tl.getParent(); // THIS IS WHAT I'D LOVE TO SEE!
  if(!master == masterTL) {
    masterTL.add(tl);
  }
}

Is there a cleaner, better way to accomplish this?

 

 

Link to comment
Share on other sites

btw, this is how I'm handling it in my app for now and it seems to work fine but not sure how reliable this is.

var kids = masterTL.getChildren(false); // just get top-level timelines that are in the masterTL


if(kids.length == 0) {
  masterTL.add(tl);
} else {
  var isThere = false;
  for ( kid in kids ) {
    if(kid == tl) {
      isThere = true;
    }
  }
  if(!isThere) {
    masterTL.add(tl);
  }
}
Link to comment
Share on other sites

Hi,

 

Since getChildren() returns an array you can do a simple indexOf() in order to check if the instance is in the parent timeline already:

var master = new TimelineMax({paused:true}),
    child1 = new TimelineMax({paused:true}),
    child2 = new TimelineMax({paused:true}),
    child3 = new TimelineMax({paused:true}),
    children;

master
  .add(child1, 1)
  .add(child2, 2);

// get childs
children = master.getChildren();

console.log( children.indexOf(child1) );// return 0
console.log( children.indexOf(child2) );// return 1
console.log( children.indexOf(child3) );// return -1 doesn't exists in the array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

  • Like 1
Link to comment
Share on other sites

I might be missing something, but wouldn't it be as easy as tl.timeline? Remember, every animation has a parent "timeline" (well, except the root timelines) and you can access that via the "timeline" property (that's read-only). 

 

So in your pseudo code, it would look like: 

//OLD:
var master = tl.getParent(); //BAD
if (!master == masterTL) {
    masterTL.add(tl);
}

//NEW:
var master = tl.timeline; //GOOD
if (!master == masterTL) {
    masterTL.add(tl);
}
  • Like 3
Link to comment
Share on other sites

ahhh, of course! Both solutions would work. Thanks. I was looking for something short and sweet. 

  • Like 1
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.
×