English 中文(简体)
How to write events for a custom component in flex?
原标题:

I have written a custom component for drawing a circle in Flex. But When I try to write a click event or mouseDown event for that component, it doesn t work.

I have the circle component inside a VBox.

 <mx:VBox label="Currents Quote" width="100%" backgroundColor="#DDDDDD">

<comp:MyCircle id="circlle" x1="175" y1="150" 
                    radius="140" click= {Alert.show("Hello");} 
                    mouseDown="handleMouseDown(event);"/>
<comp:MyLine x1="175" y1="104"/> 
 </mx:VBox>

  private function handleMouseDown(event:MouseEvent):void {

        var pt:Point = new Point(event.localX, event.localY);
        pt = event.target.localToGlobal(pt);   
        pt = circlle.globalToContent(pt);   
        var whichColor:String = "border area";

        if (pt.x < 150) {
            if (pt.y < 150)
                whichColor = "red";
            else
                whichColor = "blue";
        }
        else {
            if (pt.y < 150)
                whichColor = "green";
            else
                whichColor = "magenta";
        }

        Alert.show("You clicked on the " + whichColor);
      }

Circle component:

  package components
  {
 import mx.core.UIComponent;

 public class MyCircle extends UIComponent
 {
       public var x1:int; 
          public var y1:int; 
          public var radius:int; 

          override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void 
         { 
        graphics.lineStyle(1, 0x000000);
        graphics.drawCircle(x1, y1, radius); 

        graphics.lineStyle(1, 0x000000);
        graphics.drawCircle(x1, y1, radius-40); 

        graphics.lineStyle(1, 0x000000);
        graphics.drawCircle(x1, y1, radius-100);
         }
     }
 }

The "Hello" Alert is displayed only at a particular point and guess is, at the point, (175,150) the x, y co-ordinates of the circle. But shouldn t it be displayed wherever I click on the MyCircle component?? how to enable it in that way?

Also the mouseDown function doesn t work for MyCircle,but if have the event for the VBox, the Alerts are displayed. Why so? Can someone guide me?

Should I write events for custom components in a different manner?

最佳回答

I m not sure if this is applicable or not, but there is a bug where some UIComponents don t recognized click/mouseDown events correctly unless they have a background color. See if adding a background color to your circle helps.

问题回答

There s also a few errors in your code, for example, you re missing script tags around your actionscript, and you might need to import Alert.

 <mx:VBox label="Currents Quote" width="100%" backgroundColor="#DDDDDD">

<comp:MyCircle id="circlle" x1="175" y1="150" 
                    radius="140" click= {Alert.show("Hello");} 
                    mouseDown="handleMouseDown(event);"/>
<comp:MyLine x1="175" y1="104"/> 
 </mx:VBox>

<mx:Script>
<![CDATA[
  private function handleMouseDown(event:MouseEvent):void {

        var pt:Point = new Point(event.localX, event.localY);
        pt = event.target.localToGlobal(pt);   
        pt = circlle.globalToContent(pt);   
        var whichColor:String = "border area";

        if (pt.x < 150) {
            if (pt.y < 150)
                whichColor = "red";
            else
                whichColor = "blue";
        }
        else {
            if (pt.y < 150)
                whichColor = "green";
            else
                whichColor = "magenta";
        }

        Alert.show("You clicked on the " + whichColor);
      }
]]>
</mx:Script>




相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Multiple Remote call made simultenously

I was making multiple remote calls and they are done sequentially and when I am getting a result event back it s triggering calls to all the methods with ResultEvent as an argument . I am supposed to ...

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签