最近剛好需要一個按住按鈕連發的功能,查了一下ActionScript 3.0 Language and Components Reference確定了Flex的Buttonu有一個autoRepeat屬性,只要設為ture就可以啟動button內部的Timer,結果奇怪怎麼在按鈕按下後只觸發了一次呢?????結果一查原來我習慣使用flash.events底下的MouseEvent而autoRepeat是使用FlexEvent發送所以就收不到連發事件囉!!請看以下範例
Example:
<?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"
applicationComplete="application1_applicationCompleteHandler(event)"
>
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_applicationCompleteHandler(event:FlexEvent):void
{
//不會連發
btn.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
//會連發
btn.addEventListener(FlexEvent.BUTTON_DOWN , onButtonDown);
}
private function onMouseDown(e:MouseEvent):void
{
trace("MouseEvent.MOUSE_DOWN");
}
private function onButtonDown(e:FlexEvent):void
{
trace("FlexEvent.BUTTON_DOWN");
}
]]>
</fx:Script>
<s:Button id="btn" autoRepeat="true" label="Test"/>
</s:Application>
而當然可以利用Timer、MouseEvent.MOUSE_DOWN , MouseEvent.MOUSE_UP加上一個autoRepeat的參數來模仿連發,這樣不會被限制住只能使用Button系列元件,靈活度會較高一點。
沒有留言:
張貼留言