我做了一個簡單的測試Air應用程序,嘗試不同的方法來屏蔽或使用hitArea忽略PNG透明區域上的鼠標事件。似乎無法找到合適的組合來使其發揮作用,我也無法在網絡上找到簡潔的示例。如何使用Flex圖像忽略PNG透明區域上的鼠標操作?
點擊任何這些方法的透明區域不會導致點擊被背景處理。
這裏是我的代碼:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
status="{clicked}">
<fx:Declarations>
<s:Image id="maskX" source="@Embed('mask1.png')" cacheAsBitmap="true"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable] public var clicked:String = "none";
protected function onClick(event:MouseEvent):void
{
clicked = event.currentTarget["id"] + "\t" + (new Date()).time;
}
]]>
</fx:Script>
<!-- Some sort of background so I can see transparency working. -->
<s:Group id="background" width="100%" height="100%" click="onClick(event)">
<s:Rect width="100%" height="100%">
<s:fill>
<s:LinearGradient rotation="90">
<s:GradientEntry color="blue"/>
<s:GradientEntry color="white"/>
</s:LinearGradient>
</s:fill>
</s:Rect>
</s:Group>
<s:Group width="100%" height="100%">
<s:layout>
<s:TileLayout/>
</s:layout>
<!-- Simple attempt at having Flex respect the alpha in the PNG itself as transparent
with regard to clicks -->
<s:Group id="image1" click="onClick(event)" mouseEnabledWhereTransparent="false">
<s:Image source="@Embed('image1.png')" cacheAsBitmap="true"/>
</s:Group>
<!-- Use an explicit bitmap mask for the hitArea -->
<s:Group id="image2" click="onClick(event)" hitArea="{mask2}" mouseEnabledWhereTransparent="false">
<s:Image source="@Embed('image1.png')"/>
<s:Image id="mask2" source="@Embed('mask1.png')" cacheAsBitmap="true"/>
</s:Group>
<!-- Try using just the mask bitmap -->
<s:Group id="image3" click="onClick(event)" hitArea="{mask3}" mouseEnabledWhereTransparent="false">
<s:Image id="mask3" source="@Embed('mask1.png')" cacheAsBitmap="true"/>
</s:Group>
<!-- Specify the hitArea with alternate syntax -->
<s:Group id="image4" click="onClick(event)" mouseEnabledWhereTransparent="false">
<s:Image source="@Embed('image1.png')"/>
<s:hitArea>
<s:Image id="mask4" source="@Embed('mask1.png')" cacheAsBitmap="true"/>
</s:hitArea>
</s:Group>
</s:Group>
的圖像1和掩碼1文件,我已經在這裏上傳:
圖像1 - http://img853.imageshack.us/img853/923/image1yj.png
掩碼1 - http://img715.imageshack.us/img715/3755/mask1.png
我也嘗試使用「蒙版」,並且無法獲得蒙版。我的方法顯然有一些根本性的錯誤。 – Nimai 2012-01-05 22:57:59
我正在做研發並獲得了這個對你有用的鏈接。請訪問[鏈接](http://dougmccune.com/blog/2007/02/03/using-hittestpoint-or-hittest-on-transparent-png-images/) – 2012-01-06 10:54:37
我認爲你是對的薩加爾。因爲我已經遇到了這個問題。 – 2012-01-06 10:55:58