码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-05 14:40:05      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

此方法适用于用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     import flash.display.AVM1Movie;
  5     import flash.display.MovieClip;
  6     import flash.display.Shape;
  7     import flash.events.Event;
  8     import flash.display.Loader;
  9     import flash.net.URLRequest;
 10  
 11     /**
 12      * ...
 13      * @author Frost.Yen
 14      */
 15     public class SwfPlayer extends MovieClip 
 16     {
 17         private var _loader:Loader;
 18         private var _urlR:URLRequest;
 19         private var _url:String;
 20         private var _container:MovieClip;
 21         private var _mask:Shape;
 22         private var _forcibleLoader:ForcibleLoader;
 23         private var _stageW:Number;//swf实际的舞台宽度
 24         private var _stageH:Number;//swf实际的舞台高度
 25         public function SwfPlayer() 
 26         {
 27             //遮罩加载swf舞台
 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             _urlR=new URLRequest(_url);
 41             _loader=new Loader();
 42             _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
 43             _loader.load(_urlR);
 44         }
 45                     
 46         private function onComplete(event:Event):void {
 47             if (_loader.content is AVM1Movie) {//如果是as2.0或者1.0代码生成的swf
 48                 trace("_loader.content is AVM1Movie");
 49                 _loader.unloadAndStop();
 50                 _forcibleLoader = new ForcibleLoader(_loader);
 51                 _forcibleLoader.load(_urlR);
 52                 return;
 53             }
 54             trace(_loader.content);
 55             _container = MovieClip(_loader.content);
 56             _stageW = _loader.contentLoaderInfo.width;
 57             _stageH = _loader.contentLoaderInfo.height;
 58             _mask.width = _stageW;
 59             _mask.height = _stageH;
 60             this.addChild(_container);
 61             this.addChild(_mask);
 62             this.dispatchEvent(new Event(Event.COMPLETE));
 63         }
 64         /**
 65          * 播放下一帧
 66          */
 67         public function nextPlay():void{
 68             if(_container.currentFrame==_container.totalFrames){
 69                 stop();
 70             }
 71             else{
 72                 _container.nextFrame();
 73             }
 74         }
 75         /**
 76          * 播放上一帧
 77          */      
 78         public function prevPlay():void{
 79             if(_container.currentFrame==1){
 80                 stop();
 81             }
 82             else{
 83                 _container.prevFrame();
 84             }
 85         }
 86         /**
 87          * 开始播放
 88          */
 89         public function startPlay():void
 90         {
 91             _container.play();
 92         }
 93         /**
 94          * 暂停播放
 95          */
 96         public function pausePlay():void
 97         {
 98             _container.stop();
 99         }
100         /**
101          * 卸载加载的swf
102          */
103         public function unloadAndStop():void
104         {
105             _loader.unloadAndStop();
106         }
107       
108         public function get stageW():Number 
109         {
110             return _stageW;
111         }
112       
113         public function set stageW(value:Number):void 
114         {
115             _stageW = value;
116         }
117       
118         public function get stageH():Number 
119         {
120             return _stageH;
121         }
122       
123         public function set stageH(value:Number):void 
124         {
125             _stageH = value;
126         }
127     }
128 }

然后在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下载

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

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4315632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!