• [ActionScript 3.0] 如何控制加载swf动画的播放与暂停


    此方法适用于用as 1.0或者as2.0以及as3.0编译的swf,因为as1.0和as2.0编译的swf是AVM1Movie类型,因此需要通过类ForcibleLoader.as将其转换为version 9以上的swf,注意,如果加载的swf是3.0代码编译的,且此swf用文档类编译,则文档类必须继承MovieClip,接下来看代码:

    首先写一个加载swf的类SwfPlayer.as:

      1 package com.views
      2 {
      3     import com.controls.utils.ForcibleLoader;
      4     
      5     import flash.display.AVM1Movie;
      6     import flash.display.Loader;
      7     import flash.display.MovieClip;
      8     import flash.display.Shape;
      9     import flash.events.Event;
     10     import flash.net.URLRequest;
     11 
     12     /**
     13      * ...
     14      * @author Frost.Yen
     15      */
     16     public class SwfPlayer extends MovieClip 
     17     {
     18         private var _loader:Loader;;
     19         private var _urlR:URLRequest;
     20         private var _url:String;
     21         private var _container:MovieClip;
     22         private var _mask:Shape;
     23         private var _forcibleLoader:ForcibleLoader;
     24         private var _stageW:Number;//swf实际的舞台宽度
     25         private var _stageH:Number;//swf实际的舞台高度
     26         public function SwfPlayer() 
     27         {
     28             _mask = new Shape();
     29             _mask.graphics.beginFill(0);
     30             _mask.graphics.drawRect(0, 0, 10, 10);
     31             _mask.graphics.endFill();
     32             this.mask = _mask;
     33         }
     34         /**
     35          * 加载swf
     36          * @param    url swf路径
     37          */
     38         public function Load(url:String):void{
     39             this._url=url;
     40             if(!_loader){
     41                 _loader= new Loader()
     42             }
     43             _urlR=new URLRequest(_url);
     44             _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
     45             _loader.load(_urlR);
     46         }
     47         
     48         private function onComplete(event:Event):void {
     49             
     50             if (_loader.content is AVM1Movie) {//如果是as2.0或者1.0代码生成的swf
     51                 trace("_loader.content is AVM1Movie");
     52                 _loader.unloadAndStop();
     53                 _forcibleLoader = new ForcibleLoader(_loader);
     54                 _forcibleLoader.load(_urlR);
     55                 return;
     56             }
     57             _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
     58             trace(_loader.contentLoaderInfo,_loader,"_loader");
     59             try
     60             {
     61                 _container = MovieClip(_loader.content);
     62                 _stageW = _loader.contentLoaderInfo.width;
     63                 _stageH = _loader.contentLoaderInfo.height;
     64                 this.addChild(_container);
     65             } 
     66             catch(error:Error) 
     67             {
     68                 trace(_loader.width,"_loader.width");
     69                 
     70                 _stageW = _loader.width;
     71                 _stageH = _loader.height;
     72                 this.addChild(_loader);
     73             }
     74             
     75             _mask.width = _stageW;
     76             _mask.height = _stageH;
     77             
     78             this.addChild(_mask);
     79             this.dispatchEvent(new Event(Event.COMPLETE));
     80         }
     81         /**
     82          * 播放下一帧
     83          */
     84         public function nextPlay():void{
     85             if(_container.currentFrame==_container.totalFrames){
     86                 stop();
     87             }else{
     89                 _container.nextFrame();
     90             }
     91         }
     92         /**
     93          * 播放上一帧
     94          */      
     95         public function prevPlay():void{
     96             if(_container.currentFrame==1){
     97                 stop();
     98             }else{
    100                 _container.prevFrame();
    101             }
    102         }
    103         /**
    104          * 开始播放
    105          */
    106         public function startPlay():void
    107         {
    108             _container.play();
    109         }
    110         /**
    111          * 暂停播放
    112          */
    113         public function pausePlay():void
    114         {
    115             _container.stop();
    116         }
    117         
    118         /**
    119          * 卸载加载的swf
    120          */
    121         public function unloadAndStop():void
    122         {
    123             
    124             if(_loader){
    125                 _loader.unloadAndStop();
    126                 _loader = null;
    127                 
    128             }
    129             if(_container){
    130                 _container.parent.removeChild(_container);
    131                 _container = null;
    132             }
    133             
    134         }
    135         
    136         public function get stageW():Number 
    137         {
    138             return _stageW;
    139         }
    140         
    141         public function set stageW(value:Number):void 
    142         {
    143             _stageW = value;
    144         }
    145         
    146         public function get stageH():Number 
    147         {
    148             return _stageH;
    149         }
    150         
    151         public function set stageH(value:Number):void 
    152         {
    153             _stageH = value;
    154         }
    155         
    156     }
    157 
    158 }

    然后在flash文档类Main.as中调用,flash文档舞台上有两个控制按钮stopBtn,playBtn:

     1 package  
     2 {
     3     import com.views.SwfPlayer;
     4     import flash.display.Sprite;
     5     import flash.events.MouseEvent;
     6     /**
     7      * ...
     8      * @author Frost.Yen
     9      */
    10     public class Main extends Sprite 
    11     {
    12         private var _swfPlayer:SwfPlayer;
    13         public function Main() 
    14         {
    15             _swfPlayer = new SwfPlayer();
    16             this.addChild(_swfPlayer);
    17             _swfPlayer.load("D:/Flash学习/flash动画作品/1/1.swf");
    18             stopBtn.addEventListener(MouseEvent.CLICK,onStop);
    19             playBtn.addEventListener(MouseEvent.CLICK,onPlay);
    20         }
    21         private function onStop(e:MouseEvent):void 
    22         {
    23             _swfPlayer.pausePlay();//暂停播放
    24         }
    25         private function onPlay(e:MouseEvent):void 
    26         {
    27             _swfPlayer.startPlay();//开始播放
    28         }
    29     }
    30 }

    附:此类ForcibleLoader.as可到http://download.csdn.net/detail/yan_frost/4771007下载

  • 相关阅读:
    友元函数
    异常处理
    RTTI
    接口类
    纯虚函数和抽象类
    虚函数与虚析构函数原理
    查看表空间使用率及shrink 表空间
    RAC fail over 测试
    js判断数组中是不是有某个元素
    layui 表格图片放大
  • 原文地址:https://www.cnblogs.com/frost-yen/p/4315632.html
Copyright © 2020-2023  润新知