如何在flex中为自定义组件编写事件?
我编写了一个用于在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否适用,但有一个错误,某些 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.
您的代码中还存在一些错误,例如,您的操作脚本周围缺少脚本标记,并且您可能需要导入 Alert。
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.