![MACROMEDIA FLEX - DEVELOPING COMPONENTS AND THEMES Manual Download Page 34](http://html1.mh-extra.com/html/macromedia/flex-developing-components-and-themes/flex-developing-components-and-themes_manual_3388115034.webp)
34
Chapter 2: Creating Basic Components in Flash MX 2004
Because nearly all classes extend the UIObject class, the
initialize
event is already supported in
custom components. To define a handler for it, you add the
initialize
property to the
component’s MXML tag, and then add ActionScript code that processes the event, as the
following example shows:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
<mx:Script>
<![CDATA[
function myInit() {
trace('init greensquare');
}
]]>
</mx:Script>
<greensquare initialize="myInit();" />
</mx:Application>
Handling mouse events
All visual components that inherit from the UIObject class support a number of navigational
mouse events, including the following:
•
mouseOver
•
mouseOut
•
mouseDown
•
mouseUp
These events do not include the
click
event. For a complete list of events supported by visual
components, see the information about the control in
Developing Flex Applications
.
To use the common mouse events, you point to a handler in your MXML file. You do not need
to add any additional code to the component source code.
The following example changes the alpha (transparency) of the Green Square component
when the mouse moves over the component, and again when the mouse moves away from
the component:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
<mx:Script>
<![CDATA[
var startAlpha:Number = 40;
function myInit() {
myGS.alpha=startAlpha;
}
function changeAlpha(curAlpha:Number) {
myGS.alpha=curAlpha;
}