如何在flex中为自定义组件编写事件?

发布于 2024-08-16 17:26:31 字数 1916 浏览 7 评论 0原文

我编写了一个用于在 Flex 中绘制圆的自定义组件。但是当我尝试为该组件编写单击事件或 mouseDown 事件时,它不起作用。

我在 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);
      }

圆组件:

  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);
         }
     }
 }

“Hello”警报仅在特定点显示,猜测该点处圆的 x、y 坐标为 (175,150)。但它不应该在我点击 MyCircle 组件的任何地方显示吗?如何以这种方式启用它?

另外,mouseDown 函数不适用于 MyCircle,但如果有 VBox 的事件,则会显示警报。为什么会这样呢?有人可以指导我吗?

我应该以不同的方式为自定义组件编写事件吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2024-08-23 17:26:31

我不确定这是否适用,但有一个错误,某些 UIComponent 无法正确识别 click/mouseDown 事件,除非它们有背景颜色。看看为圈子添加背景颜色是否有帮助。

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.

黒涩兲箜 2024-08-23 17:26:31

您的代码中还存在一些错误,例如,您的操作脚本周围缺少脚本标记,并且您可能需要导入 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文