Creating the Dial class file

Now, you need to create the Dial class file as a new ActionScript file.

To create the Dial class file:

  1. In Flash, select File > New and then select ActionScript File.
  2. Select File > Save As and save the file as Dial.as in the same folder as the Dial.fla file.

    NOTE

    You can use any text editor to save the Dial.as file.

  3. You can copy or type the following Dial component ActionScript class code into your new Dial.as file. Typing rather than copying the code helps you become familiar with each element of the component code.

    Please read the comments in the code for a description of each section. (For detailed information on the elements of a component class file, see Overview of a component class file.

    // Import the package so you can reference 
    // the class directly.
    import mx.core.UIComponent;
    
    // Event metadata tag 
    [Event("change")]
    class Dial extends UIComponent
    {
        // Components must declare these to be proper
        // components in the components framework.
        static var symbolName:String = "Dial";
        static var symbolOwner:Object = Dial;
        var className:String = "Dial";
    
        // The needle and dial movie clips that are
        // the component's graphical representation
        private var needle:MovieClip;
        private var dial:MovieClip;
        private var boundingBox_mc:MovieClip;
    
        // The private member variable "__value" is publicly
        // accessible through implicit getter/setter methods,
        // Updating this property updates the needle's position
        // when the value is set.
        private var __value:Number = 0;
    
        // This flag is set when the user drags the
        // needle with the mouse, and cleared afterwards.
        private var dragging:Boolean = false;
    
        // Constructor; 
        // While required for all classes, v2 components require
        // the contstructor to be empty with zero arguments. 
        // All initialization takes place in a required init() 
        // method after the class instance has been constructed.
        function Dial() {
        }
    
        // Initialization code:
        // The init() method is required for v2 components. It must also
        // in turn call its parent class init() method with super.init().
        // The init() method is required for components extending UIComponent.
        function init():Void {
            super.init();
            useHandCursor = false;
            boundingBox_mc._visible = false;
            boundingBox_mc._width = 0;
            boundingBox_mc._height = 0;
        }
        // Create children objects needed at start up:
        // The createChildren() method is required for components
        // extending UIComponent.
        public function createChildren():Void {
            dial = createObject("DialFinal", "dial", 10);
            size();
        }
    
        // The draw() method is required for v2 components.
        // It is invoked after the component has been
        // invalidated by someone calling invalidate().
        // This is better than redrawing from within the set() function
        // for value, because if there are other properties, it's
        // better to batch up the changes into one redraw, rather
        // than doing them all individually. This approach leads
        // to more efficiency and better centralization of code.
        function draw():Void {
            super.draw();
            dial.needle._rotation = value;
        }
    
        // The size() method is invoked when the component's size
        // changes. This is an opportunity to resize the children,
        // and the dial and needle graphics.
        // The size() method is required for components extending UIComponent.
        function size():Void {
            super.size();
            dial._width = width;
            dial._height = height;
            // Cause the needle to be redrawn, if necessary.
            invalidate();
        }
    
        // This is the getter/setter for the value property.
        // The [Inspectable] metadata makes the property appear
        // in the Property inspector. This is a getter/setter
        // so that you can call invalidate and force the component
        // to redraw, when the value is changed.
        [Bindable]
        [ChangeEvent("change")]
        [Inspectable(defaultValue=0)]
        function set value (val:Number)
        {
            __value = val;
            invalidate();
        }
    
        function get value ():Number
        {
            return twoDigits(__value);
        }
    
        function twoDigits(x:Number):Number
        {
            return (Math.round(x * 100) / 100);
        }
    
    
        // Tells the component to expect mouse presses
        function onPress()
        {
            beginDrag();
        }
    
        // When the dial is pressed, the dragging flag is set.
        // The mouse events are assigned callback functions.
        function beginDrag()
        {
            dragging = true;
            onMouseMove = mouseMoveHandler;
            onMouseUp = mouseUpHandler;
        }
    
        // Remove the mouse events when the drag is complete
        // and clear the flag.
        function mouseUpHandler()
        {
            dragging = false;
            delete onMouseMove;
            delete onMouseUp;
        }
    
        function mouseMoveHandler()
        {
            // Calculate the angle
            if (dragging) {
                var x:Number = _xmouse - width/2;
                var y:Number = _ymouse - height/2;
    
                var oldValue:Number = value;
                var newValue:Number = 90+180/Math.PI*Math.atan2(y, x);
                if (newValue<0) {
                    newValue += 360;
                }
                if (oldValue != newValue) {
                    value = newValue;
                    dispatchEvent( {type:"change"} );
                }
            }
        }
    }