animation.isPlaying returning TRUE when animation complete

Hey all,

I was trying to put together a simple example of opening and closing a lid to a box today… both sequences (OpenLid CloseLid) were keyframed in Cheetah and imported.

If the user clicks on the lid, I want it to run the open sequence… but ignore the click if it is in the process of animating. I thought I had a good idea of how to script this, but once I click and start the open sequence, I can’t get animation.isPlaying to return anything but TRUE when it has completed. Shouldn’t there be a callback of some kind when the last keyframe is reached to toggle this variable back to FALSE?

Any help would be appreciated:

private var lidClosed = 1;

function OnMouseDown() {	
	if (!animation.isPlaying)
	{
		if (lidClosed)
		{
			animation.Play("OpenLid");
			lidClosed = 0;
		} else {
			animation.Play("CloseLid");
			lidClosed = 1;
		}
	}
}

I’m assuming your wrap mode is set to clamp forever, so the animation maintains its last frame even after it’s done playing? If this is the case, your animation is still considered “playing” while it sits their maintaining its final frame. An animation with a wrap mode of Once should stop returning TRUE for isPlaying after it completes… but in the case of an opening lid, it would also pop back to being closed after finishing.

You may want to consider checking the normalizedTime of your animation clip instead of just checking to see if any animation is playing at all. A value >= 1 would mean the animation was finished.

Thx Animator,

I actually have the animation set to “Once” and it is still returning TRUE when I click on the lid (where the script is attached) after the opening sequence completes. I’m sure I could get some iteration of the normalizedTime solution to work, but it would be alot cleaner if I could figure out why the isPlaying is not reinitializing.

Thanks :slight_smile:

MJ

That code works as expected here (although it would be better practice to use actual booleans for boolean values; i.e., true and false instead of 1 and 0). I’m not familiar with Cheetah, but are you sure the animation is actually finished after the lid opens? Technically it’s possible for the animation clip to continue even if nothing is moving, if that’s the way it’s set up.

–Eric

@Eric

Good point about the booleans (changed :)). I also found my problem was timeline related in Cheetah. I had the length of my animation take set to the number of frames instead of seconds, so… logically the timeline was still processing when I thought it should be done. Works like a charm now.

Thanks for the help.

Matt