This article introduces how to create a custom component in <flash 8 Event Management> 30th.
In this example, a small column of a slide bar is implemented.
Step 1. Create a flash file named slider. Fla. Save it in the slider001 directory.
Step 2. Create a movieclip component named slider in the document.
Step 3. Create a slider class and save it in the slider001 \ actionscriptbible \ components directory
// Declare a class named slider. This class is located in the actionscriptbible. Components Package.
// This class should protect movieclip.
Class actionscriptbible. components. Slider extends movieclip
{
// The slider component consists of a flip bar and
// A track that moves with the flip bar
Private VaR _ mthumbbar: movieclip;
Private VaR _ mtrack: movieclip;
// Define a private property to store the current value of the slider component.
// The value range is 0-100.
Private VaR _ nvalue: number;
// This component uses a listener object to detect mouse movement.
Private VaR _ omouselistener: object;
// The component uses the following two attributes to determine the width and height of the component instance.
Private VaR _ nwidth: number;
Private VaR _ nheight: number;
// Define a "retrieve function" Method
// You can use the value attribute name to access _ nvalue in public.
Public Function get value (): Number
{
Return _ nvalue;
}
// Define a constructor. When a new instance of this component is created,
// The constructor is automatically called.
Public Function slider ()
{
// Define the default value of width and height.
_ Nwidth = 200;
_ Nheight = 10;
// Initialize _ nvalue to 0.
If (_ nvalue = undefined)
{
_ Nvalue = 0;
}
// Create a video clip for the flip bar and track
_ Mtrack = createemptymovieclip ("_ mtrack", getnexthighestdepth ());
_ Mthumbbar = createemptymovieclip ("_ mthumbbar", getnexthighestdepth ());
// Draw the component.
Draw ();
// Define the listener object to detect mouse movement events.
// The Listener object calls the update () method of the slider instance.
_ Omouselistener = new object ();
_ Omouselistener. onmousemove = mx. utils. Delegate. Create (this, update );
// Give the movieclip object named mthumbbar
// Define the onpress () method and onrelease () method.
_ Mthumbbar. onpress = mx. utils. Delegate. Create (this, onpressthumbbar );
_ Mthumbbar. onrelease = mx. utils. Delegate. Create (this, onreleasethumbbar );
_ Thinkumbbar. onreleaseoutside = _ thinkumbbar. onrelease;
}
Private function draw (): void
{
// Clear all previously drawn content.
_ Mthumbbar. Clear ();
_ Mtrack. Clear ();
// Draw a flip bar with a width of 5 pixels and a height specified by _ nheight.
_ Mthumbbar. linestyle (0, 0 );
_ Mthumbbar. beginfill (0,100 );
_ Mthumbbar. lineto (5, 0 );
_ Mthumbbar. lineto (5, _ nheight );
_ Mthumbbar. lineto (0, _ nheight );
_ Mthumbbar. lineto (0, 0 );
_ Mthumbbar. endfill ();
// Draw a 10-pixel track with a width specified by _ nwidth.
_ Mtrack. linestyle (100 );
_ Mtrack. lineto (_ nwidth, 0 );
_ Mtrack. lineto (_ nwidth, 5 );
_ Mtrack. lineto (0, 5 );
_ Mtrack. lineto (0, 0 );
// Place the orbit in the vertical center.
_ Mtrack. _ y = _ height/2-2.5;
}
Private function onpressthumbbar (): void
{
// When the user clicks the mouse on the object,
// Make the object scalable within the specified range.
_ Mthumbbar. startdrag (false, 0, 0, _ mtrack. _ width-_ mthumbbar. _ width +. 5, 0 );
// Tell the component to start detecting mouse movement events.
Mouse. addlistener (_ omouselistener );
}
Private function onreleasethumbbar (): void
{
// When the user releases the flip bar, the flash will be told to stop dragging the flip bar.
_ Mthumbbar. stopdrag ();
// Tell the widget to stop detecting mouse movement events.
Mouse. removelistener (_ omouselistener );
Update ();
}
// Call the update () method as long as you press the flip bar and move the mouse.
// This method only changes _ nvalue to reflect the current X coordinate of the page flip bar.
Private function Update (): void
{
_ Nvalue = math. Round (_ mthumbbar. _ x)/(_ mtrack. _ width +. 5-_ mthumbbar. _ width) * 100 );
}
}
Step 4. In the flash document, set the slider component link identifier to slider. As 2.0 class: actionscriptbible. components. Slider is the fully qualified name.
Step 5. Create an instance of the slider component named sldexample
Step 6. Create a actions layer and add the following code to the first frame:
Setinterval (checkvalue, 1000 );
Function checkvalue (): void
{
Trace (sldexample. value );
}
Step 7. Test. You should see the slider instance and the value displayed in the output panel. You can change this value when you drag the slider.