This site is optimised for Firefox 2.0 or Internet Explorer 7.

Calculate number of frames between two Labels

onpurpos has no avatar
onpurpos
 
Post Count: 16
Joined: Tue May 13, 2008 1:28 am

Calculate number of frames between two Labels

Postby onpurpos on Tue Mar 10, 2009 7:21 pm

I'm trying to create a function that looks at two Frame Labels and calculates the number of frames between them. This is what I've got so far:

Code: Select all
function convertLabelsToFrames (targetClip:MovieClip, startFrameLabel:String, endFrameLabel:String):int
{
   var time:int = targetClip.endFrameLabel.frame - targetClip.startFrameLabel.frame;
   return time;
};


The problem I'm having is that you don't seem to be able to find a FrameLabel within a MovieClip in the simple sort of way that I've used above (I know the way I specify here won't work). It does seem, though, like there should be SOME way of finding a FrameLabel within a MovieClip. Anybody know how to do this?

jschertz has no avatar
jschertz
 
Post Count: 8
Joined: Mon Mar 09, 2009 6:27 pm
Location: Chicago
Flash Version: Adobe Flash CS4

Re: Calculate number of frames between two Labels

Postby jschertz on Tue Mar 10, 2009 8:20 pm

Took a bit of research but I figured it out. Here's the code:
Code: Select all
import flash.display.FrameLabel;

private function convertLabelsToFrames(targetClip:MovieClip, startFrameLabel:String, endFrameLabel:String):int {
         
         var frameLabelsArray:Array = targetClip.currentLabels;
         var startLabel:FrameLabel;
         var endLabel:FrameLabel;
         var n:int = frameLabelsArray.length;
         
         for (var i:int = 0; i < n; i++){
            
            if(frameLabelsArray[i].name == startFrameLabel){
               startLabel = frameLabelsArray[i];
            }
            
            if(frameLabelsArray[i].name == endFrameLabel){
               endLabel = frameLabelsArray[i];
            }
            
         }
         
         var time:int = endLabel.frame - startLabel.frame;
         
         return time;
         
         
      }


EDIT: Here's something super dope. Alliteratively, we can code the return value as such:

Code: Select all
return int(Math.abs(time));


I'm taking the absolute value of time Math.abs(time) which returns a positive Number and casting it as an int to maintain the function's return value integrity. What does this all mean? It means that even if time is a negative number it will always return a positive integer, so you won't have to worry about which label is the start or end label. :D


Hope this helps.
Follow me on twitter: @jschertz
or go to my site: God Save The Tween

onpurpos has no avatar
onpurpos
 
Post Count: 16
Joined: Tue May 13, 2008 1:28 am

Re: Calculate number of frames between two Labels

Postby onpurpos on Sat Apr 18, 2009 8:11 pm

I never responded to tell you that your code is super dope. Well done!


Return to Actionscript 3.0

Who is online

Users browsing this forum: Kirone and 5 guests