FrameUtil

Kind of class: public class
Package:
Inherits from:
  • none
Version: 02/12/10
Author: Mike Creighton, Aaron Clinger
Classpath: org.casalib.util.FrameUtil
File last modified: Friday, 20 May 2011, 00:59:45
► View source▼ Hide source
/*
    CASA Lib for ActionScript 3.0
    Copyright (c) 2011, Aaron Clinger & Contributors of CASA Lib
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    
    - Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    
    - Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    
    - Neither the name of the CASA Lib nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/
package org.casalib.util {
    import flash.display.MovieClip;
    import org.casalib.errors.ArguementTypeError;
    
    
    /**
        Utilities for determining label positions and adding and removing frame scripts.
        
        @author Mike Creighton
        @author Aaron Clinger
        @version 02/12/10
    */
    public class FrameUtil {
        
        
        /**
            Determines the frame number for the specified label.
            
            @param target: The MovieClip to search for the frame label in.
            @param label: The name of the frame label.
            @return The frame number of the label or -1 if the frame label was not found.
        */
        public static function getFrameNumberForLabel(target:MovieClip, label:String):int {
            var labels:Array = target.currentLabels;
            var l:int        = labels.length;
            
            while (l--)
                if (labels[l].name == label)
                    return labels[l].frame;
            
            return -1;
        }
        
        /**
            Calls a specified method when a specific frame is reached in a MovieClip timeline.
            
            @param target: The MovieClip that contains the frame.
            @param frame: The frame to be notified when reached. Can either be a frame number (uint), or the frame label (String).
            @param notify: The function that will be called when the frame is reached.
            @return Returns true if the frame was found; otherwise false.
            @throws ArguementTypeError if you pass a type other than a String or uint to parameter frame.
        */
        public static function addFrameScript(target:MovieClip, frame:*, notify:Function):Boolean {
            if (frame is String)
                frame = FrameUtil.getFrameNumberForLabel(target, frame);
            else if (!(frame is uint))
                throw new ArguementTypeError('frame');
            
            if (frame == -1 || frame == 0 || frame > target.totalFrames)
                return false;
            
            target.addFrameScript(frame - 1, notify);
            
            return true;
        }
        
        /**
            Removes a frame from triggering/calling a function when reached.
            
            @param target: The MovieClip that contains the frame.
            @param frame: The frame to remove notification from. Can either be a frame number (uint), or the frame label (String).
            @throws ArguementTypeError if you pass a type other than a String or uint to parameter frame.
        */
        public static function removeFrameScript(target:MovieClip, frame:*):void {
            if (frame is String)
                frame = FrameUtil.getFrameNumberForLabel(target, frame);
            else if (!(frame is uint))
                throw new ArguementTypeError('frame');
            
            if (frame == -1 || frame == 0 || frame > target.totalFrames)
                return;
            
            target.addFrameScript(frame - 1, null);
        }
    }
}
Utilities for determining label positions and adding and removing frame scripts.

Summary

Class methods
  • getFrameNumberForLabel (target:MovieClip, label:String) : int
    • Determines the frame number for the specified label.
  • addFrameScript (target:MovieClip, frame = *, notify:Function) : Boolean
    • Calls a specified method when a specific frame is reached in a MovieClip timeline.
  • removeFrameScript (target:MovieClip, frame = *) : void
    • Removes a frame from triggering/calling a function when reached.

Class methods

addFrameScript

static function addFrameScript(target:MovieClip, frame = *, notify:Function) : Boolean

Calls a specified method when a specific frame is reached in a MovieClip timeline.

Parameters
target:The MovieClip that contains the frame.
frame :The frame to be notified when reached. Can either be a frame number (uint), or the frame label (String).
notify:The function that will be called when the frame is reached.
Returns
  • Returns true if the frame was found; otherwise false.
Throws

getFrameNumberForLabel

static function getFrameNumberForLabel(target:MovieClip, label:String) : int

Determines the frame number for the specified label.

Parameters
target:The MovieClip to search for the frame label in.
label :The name of the frame label.
Returns
  • The frame number of the label or -1 if the frame label was not found.

removeFrameScript

static function removeFrameScript(target:MovieClip, frame = *) : void

Removes a frame from triggering/calling a function when reached.

Parameters
target:The MovieClip that contains the frame.
frame :The frame to remove notification from. Can either be a frame number (uint), or the frame label (String).
Throws