2012-04-17 25 views
0

如何僅在與組件的可見部分交互時才使Flex調度鼠標事件?在這種情況下,我希望只在懸停線時才能分派事件。鼠標事件僅在可見部件上

<s:Group mouseOver="trace('over')"> 
    <s:Line xFrom="0" yFrom="0" xTo="100" yTo="100"> 
     <s:stroke> 
      <s:SolidColorStroke color="0" weight="3"/> 
     </s:stroke> 
    </s:Line> 
</s:Group> 

我記得我在Flex 3的前一段時間有一個問題,當我沒能趕上鼠標事件,直到我充滿透明背景的畫布。但是現在我有相反的問題。感謝幫助。

+0

嘗試mouseChildren =假; – 2012-04-17 18:59:44

回答

0

你在正確的軌道上。

正如你已經完成,你需要用一個GroupLine對象,或其他一些容器類,因爲星火基本圖形(如Line)不調度鼠標事件。

我想你的問題是,由於您的線路是對角線,爲Group邊框是一個矩形,它是比線下大得多。

如果繪製水平或垂直線,則Group的邊界框應該只是Line的尺寸。然後旋轉Group以獲得對角線:

請注意,我剛挑選了一個隨機的X值和旋轉......旋轉具有取代對象的X/Y座標的效果......除非您使用BasicLayout以外的東西。所以你必須調整X/Y /旋轉(和/或佈局)來將你的線放在正確的位置。

<s:Group mouseOver="trace('over')" rotation="45"> 
    <s:Line x="100" yFrom="0" yTo="100"> 
     <s:stroke> 
      <s:SolidColorStroke color="0" weight="3"/> 
     </s:stroke> 
    </s:Line> 
</s:Group> 
0

你可以試試下面的代碼: -

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import mx.controls.Alert; 

      private function rollOver(event:MouseEvent):void 
      { 
       Alert.show('over'); 
      } 

      private function drawLine():void 
      { 
       var g:Graphics = graphics; 

       graphComp.graphics.clear(); 
       var strokeColor:Number = getStyle("strokeColor"); 
       var shadowColor:Number = getStyle("shadowColor"); 

       graphComp.graphics.beginFill(strokeColor); 
       graphComp.graphics.drawRect(0, 0, 100, 1); 
       graphComp.graphics.endFill(); 

       graphComp.graphics.beginFill(shadowColor); 
       graphComp.graphics.drawRect(0, 1, 100, 1); 
       graphComp.graphics.endFill(); 
       graphComp.rotation = 45; 

      } 
     ]]> 
    </fx:Script> 
    <s:Group id="graphComp" x="100" y="100" creationComplete="drawLine()" rollOver="rollOver(event)"/> 

</s:Application>