在Flex应用程序中ActionScript脚本的使用是必不可少的,两者的交互也是少不了的,总结一些在Flex中使用AS大致有以下三中形式。
1、直接写在MXML代码中。
即将处理事件逻辑的AS脚本写在MXML的Script标记中:
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< mx:Application xmlns:mx = " http://www.adobe.com/2006/mxml " layout = " absolute " >
< mx:Panel x = " 10 " y = " 10 " width = " 403 " height = " 385 " layout = " absolute " >
< mx:VideoDisplay x = " 10 " y = " 10 " width = " 363 " height = " 275 " id = " player " source = " file:///D|/Flex/Flv/1.flv " autoPlay = " false " borderColor = " #0000ff " borderStyle = " solid " />
< mx:Button x = " 202 " y = " 313 " label = " Play " id = " btnPlay " click = " onPlay(); " />
< mx:Button x = " 286 " y = " 313 " label = " Stop " id = " btnStop " click = " player.stop(); " />
</ mx:Panel >
< mx:Script >
<! [CDATA[
private function onPlay(): void
{
player.play();
}
]] >
</ mx:Script >
</ mx:Application >
< mx:Application xmlns:mx = " http://www.adobe.com/2006/mxml " layout = " absolute " >
< mx:Panel x = " 10 " y = " 10 " width = " 403 " height = " 385 " layout = " absolute " >
< mx:VideoDisplay x = " 10 " y = " 10 " width = " 363 " height = " 275 " id = " player " source = " file:///D|/Flex/Flv/1.flv " autoPlay = " false " borderColor = " #0000ff " borderStyle = " solid " />
< mx:Button x = " 202 " y = " 313 " label = " Play " id = " btnPlay " click = " onPlay(); " />
< mx:Button x = " 286 " y = " 313 " label = " Stop " id = " btnStop " click = " player.stop(); " />
</ mx:Panel >
< mx:Script >
<! [CDATA[
private function onPlay(): void
{
player.play();
}
]] >
</ mx:Script >
</ mx:Application >
如代码中的,将btnPlay按钮的click事件处理放在了Script标记中。
2、将AS脚本单独写在一个.as文件中:
MXML文件为:
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< mx:Application xmlns:mx = " http://www.adobe.com/2006/mxml " layout = " absolute " >
< mx:Panel x = " 10 " y = " 10 " width = " 403 " height = " 385 " layout = " absolute " >
< mx:VideoDisplay x = " 10 " y = " 10 " width = " 363 " height = " 275 " id = " player " source = " file:///D|/Flex/Flv/1.flv " autoPlay = " false " borderColor = " #0000ff " borderStyle = " solid " />
< mx:Button x = " 202 " y = " 313 " label = " Play " id = " btnPlay " click = " onPlay(); " />
< mx:Button x = " 286 " y = " 313 " label = " Stop " id = " btnStop " click = " player.stop(); " />
</ mx:Panel >
< mx:Script source = " EditHandle.as " />
</ mx:Application >
< mx:Application xmlns:mx = " http://www.adobe.com/2006/mxml " layout = " absolute " >
< mx:Panel x = " 10 " y = " 10 " width = " 403 " height = " 385 " layout = " absolute " >
< mx:VideoDisplay x = " 10 " y = " 10 " width = " 363 " height = " 275 " id = " player " source = " file:///D|/Flex/Flv/1.flv " autoPlay = " false " borderColor = " #0000ff " borderStyle = " solid " />
< mx:Button x = " 202 " y = " 313 " label = " Play " id = " btnPlay " click = " onPlay(); " />
< mx:Button x = " 286 " y = " 313 " label = " Stop " id = " btnStop " click = " player.stop(); " />
</ mx:Panel >
< mx:Script source = " EditHandle.as " />
</ mx:Application >
AS文件为:
private
function
onPlay():
void
{
player.play();
}
{
player.play();
}
通过在MXML文件中将Script的Source属性设置为对应的文件即可。
3、当处理脚本比较复杂,可以使用类来分解,这样对于类的使用可以在2的基础上进行:
MXML文件同2;
AS调用文件:
//
ActionScript file
import snow.Try;
private function onPlay(): void
{
Try.play(player);
}
import snow.Try;
private function onPlay(): void
{
Try.play(player);
}
AS类文件:
package snow
{
import mx.controls.VideoDisplay;
public class Try
{
public static function play(v:VideoDisplay): void
{
v.play();
}
}
}
{
import mx.controls.VideoDisplay;
public class Try
{
public static function play(v:VideoDisplay): void
{
v.play();
}
}
}