multiple iTweens causing havoc!

Hi there,
I’ve been running into this little issue with iTween and I was wondering if anyone knew a way around it…

I’m using iTween to create animation on a button both when the user presses and releases. The problem is that if the initial press animation hasn’t finished when the release one starts, things go a little wierd and sometimes the release one doesn’t play…

I tried putting something like this in my function to delete the itween component if there was one there

	if (GetComponent (iTween)){
	Destroy (GetComponent (iTween));
	}

Any ideas?
Thanks
Pete

You really just need a flag in there to determine if movement is currently taking place, and a yield for the length of the animation?

For example (door specific and probably not GREAT code):

private var doorIsMoving : boolean = false;
private var doorIsOpen : boolean = false;

function Update () {
	Whatever your check is for the button press - e.g. maybe a raycast {
		if(!doorIsMoving) {
			if(doorIsOpen) {
				CloseDoor();
			}
			else {
				OpenDoor();
			}
		}
	}
}

function OpenDoor() {
	doorIsOpen = true;
	doorIsMoving = true;
	iTween.moveBy(frameObject,{"y":7});
	yield WaitForSeconds(1);
	doorIsMoving = false;
}

function CloseDoor() {
	doorIsOpen = false;
	doorIsMoving = true;
	iTween.moveBy(frameObject,{"y":-7});
	yield WaitForSeconds(1);
	doorIsMoving = false;
}

Thanks geppetto,
The sample code you have there will stop a second iTween from running by checking if it is already running but I would like the second iTween to override the first one. It’s more important for me that the released animation plays than the pressed one.

I realised what I was doing wrong with my script, I wasn’t assigning a gameobject for the getcomponent bit. oops…

Now I have this to remove the exsting itweens before adding a new one (i made it a while just incase there are more than one for some reason. Works great

	while (WhateverGameObject.GetComponent (iTween)){
		Destroy (G_Object.GetComponent (iTween));
		yield;
   	}

Thanks for the help!

I’m a little late to this. But just as a heads up iTween does have Stop() methods. You can stop all iTweens of a certain type, all iTweens in a scene, and a few other approaches. Just wanted to share. .

Ahh,
I think I’ll stick to my little brute force delete technique but I’ll check out those methods for sure!
Thanks Again!
Pete

Ah… sorry about that. I misunderstood exactly what you were trying to do.

I had similar problems when trying to create new identical iTweens to replace the old ones (still running).

I’m using iTween 2.x c# version.

When I tried to remove iTween with .Stop() -methods and create new ones right after. It did not work as expected.

Component[] tweens = GetComponents(typeof(iTween));	
foreach (iTween item in tweens)
{	
  Debug.Log("exists: "+item.id);		
}

Debug.Log("Stopping iTweens");	
iTween.Stop(gameObject, true);	

Component[] tweens2 = GetComponents(typeof(iTween));	
foreach (iTween item in tweens2)
{		
  Debug.Log("exists: "+item.id);		
}

The resulted output was:

exists: ZRZzMGrgfjEeUkd
exists: hJhSQXece4mxfpX
Stopping iTweens
exists: ZRZzMGrgfjEeUkd
exists: hJhSQXece4mxfpX

I also tried Destroy() and Dispose() -styled approaches and they didn’t work.

I’m assuming this has to do with Unity not removing components right away, but after frame has completed. This is just assumption. Could be totally different or some misunderstanding on my part. Or maybe it needs some kind of flushing?

So if you try to remove the old tween and create a identical one inside the same frame, what happens (or what I think happens), is that Unity marks the tween to be deleted later on. Then, while trying to add new tween, the iTween system checks for duplicates and finds the old tween still breathing and doesn’t create a new instance. After the frame, the old one gets deleted by Unity.

Or, maybe I’m just confused and doing things the way I’m not supposed to.

I found that the most quickest hack to fix this is quite elegant: You just put a little random salt in your new iTween to make it differ from the old one. For example this does the trick:

"time", timing + Random.Range(0f,0.1f)

Nice deduction! iTween operates on the principle that it “doesn’t destroy duplicates”. I switched to this style since a ton of people were using iTween. 1.0.xx in Update loops and the repeated calls would not allow the animation to actually occur.

Your solution should work fine. Though I have had success with the Stop() methods myself. I’ll take a look at them and see if I can get them running better.

Off the top of my head I do recall needing to stop on MouseDown and start a new iTween on MouseUp on the ValueTo example on the support site. I guess it has to do with the event in the same frame as you said.