Using ActionScript in Flex

在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>

如代码中的,将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>

AS文件为:

private function onPlay():void
{
    player.play();
}

通过在MXML文件中将Script的Source属性设置为对应的文件即可。

3、当处理脚本比较复杂,可以使用类来分解,这样对于类的使用可以在2的基础上进行:

MXML文件同2;

AS调用文件:

// ActionScript file
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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值