Controls the loading of SwF animations to pause and play

Source: Internet
Author: User
Tags addchild

Externally loaded SWF animations (whether as2.0 or as3.0) can control pause and play, sound can pause and play, but note that the imported sound must be "data flow". Take a look at the following code:

Main.as:

Package{import flash.display.sprite;import flash.events.mouseevent;import flash.text.textfield;/** * @author Hypo.chen *@e-mail [email protected] * @data 2015-9-6 */[swf (framerate= "", width= "1024x768", height= "768", Backgroundcolor= "0x669966")]public class Main extends sprite{private var _loadswf:loadswfhandler;private var _playBtn: textfield;private var _stopbtn:textfield;public function Main () {_loadswf = new Loadswfhandler (); This.addchild (_ LOADSWF); _loadswf.loadswf ("assest/testanimation.swf"); This.addchild (SetText ("pause", 5,700,stop)); This.addchild ( SetText ("Play", 50,700,go));} Private function Go (e:mouseevent): Void{_loadswf.targetplay ();} Private function Stop (e:mouseevent): Void{_loadswf.targetpause ();} Private Function SetText (_text:string,_x:number = 0,_y:number = 0,_fun:function = null): Textfield{var _textfield: TextField = new TextField (); _textfield.text = _text;_textfield.selectable = False;_textfield.autosize = "Left"; _ textfield.x = _x;_textfield.y = _y;_textfield.addeventlistener (mouseeveNt. Click,_fun); return _textfield;}}}

Loadswfhandler.as:

Package{import Com.cndragon.api.api.utils.forcibleloader;import Flash.display.avm1movie;import Flash.display.loader;import Flash.display.movieclip;import Flash.display.shape;import Flash.events.Event;import flash.net.urlrequest;/** * @author hypo.chen *@e-mail [email protected] * @data 2015-9-6 */public class Loadswfhandler extends movieclip{private var _mask:shape;private var _stagew:number;private var _stageh:number;private var _loader:loader;private var _forcibleloader:forcibleloader;private var _targetmc:movieclip;private var _url:String ;p rivate var _urlr:urlrequest;public function Loadswfhandler () {_mask = new Shape (); _mask.graphics.beginfill (0XFFFFFF, 1); _mask.graphics.drawrect (0,0,10,10); _mask.graphics.endfill (); this.mask = _mask;} Public Function loadswf (_url:string): void{//Load Swfthis._url = _url;if (!_loader) _loader = new loader (); _URLR = new URLRequest (_url); _loader.contentloaderinfo.addeventlistener (event.complete,onswfloaded); _loader.load (_urlR);} Private Function onswfloaded (e:event): Void{if (_loader.content is Avm1movie)////if it is as2.0 or 1.0 code-generated SWF then it needs to be processed by Forcibleloader {Trace ("_ Loader.content is Avm1movie "); _loader.unloadandstop (); _forcibleloader = new Forcibleloader (This._loader); _ Forcibleloader.load (THIS._URLR); return;//This return must be added, otherwise load as2.0 or error}_ Loader.contentLoaderInfo.removeEventListener (event.complete,onswfloaded); Trace (_loader.content,_loader, "_ Loader "); Try{trace (" try ") _TARGETMC = MovieClip (_loader.content); this._stagew = _loader.contentloaderinfo.width; This._stageh = _loader.contentloaderinfo.height;this.addchild (_TARGETMC); trace (_targetmc.totalframes);} catch (Error:error) {trace ("Error---------:" + error); This._stagew = _loader.width;this._stageh = _loader.height; This.addchild (_loader);} _mask.width = This._stagew;_mask.height = This._stageh;this.addchild (_mask);} /** * Unload loaded Swf*/public function Unloadandstop (): Void{if (_loader) {_loader.unloadandstop (); _loader = null;} if (_TARGETMC) {_targetmc.parent.removechild (_TARGETMC); _TARGETMC = null;}} public function Targetpause (): Void{_targetmc.stop ();} Public Function Targetplay (): Void{_targetmc.play ();}}}

There is also a forcibleloader.as class that can be downloaded from the Internet.

Controls the loading of SwF animations to pause and play

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.