游戏中经常使用到SWF美术,但SWF文件中,会有多个元件,每个元件对应一个指定AS链接,一个元件还会有多个图层。在实际操作中,会出现要求显示一个指定元件,并在该动画资源中,有的动画要求加鼠标事件,有的则不加任何事件,即指定图层的调用。既要求过场动画的欢洽,又要求交互事件的鼠标点击时的动画效果以及配音,这里提供一个简单范例,实现代码如下:
SWF文件加载类,LoaderSwf:
1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.BitmapData; 5 import flash.display.Loader; 6 import flash.display.LoaderInfo; 7 import flash.display.MovieClip; 8 import flash.display.SimpleButton; 9 import flash.display.Sprite; 10 import flash.events.Event; 11 import flash.events.MouseEvent; 12 import flash.net.URLRequest; 13 import flash.system.ApplicationDomain; 14 import flash.system.LoaderContext; 15 16 // 参考网址:http://blog.csdn.net/jrg_2008/article/details/49496185 17 18 [SWF(frameRate=‘25‘,width="1280", height="793")] 19 public class LoaderSwf extends Sprite 20 { 21 private var _MovLoader:Loader; // 加载swf文件 22 private var _Skin:MovieClip; // 指定AS链接 23 private var _audio:VoicePlay = new VoicePlay(); 24 private var _xmlList:XmlProject = new XmlProject(); 25 private var count:int = 0; 26 27 28 //构造函数 29 public function LoaderSwf() 30 { 31 loadMcSWF(); 32 } 33 34 //加载swf 35 private function loadMcSWF():void 36 { 37 _MovLoader = new Loader; 38 39 /* 新建应用程序域 */ 40 var context:LoaderContext = new LoaderContext(); 41 context.applicationDomain = new ApplicationDomain(); 42 43 /* 开始加载swf */ 44 _MovLoader.load(new URLRequest("U2_amazed.swf"),context); 45 46 /* 添加事件响应,在swf加载完毕后再获取里面的资源 */ 47 _MovLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete); 48 } 49 50 private function onComplete(e:Event):void 51 { 52 /* 获得加载器的应用程序域 */ 53 var domain:ApplicationDomain = (e.currentTarget as LoaderInfo).applicationDomain; 54 55 /* 获取指定(b3u3p2_1a)的AS链接的影片剪辑 */ 56 var cls:Class = domain.getDefinition("b3u2p2_1a") as Class; 57 _Skin = new cls() as MovieClip; 58 _Skin.x = 140; 59 _Skin.y = 0; 60 _Skin.addEventListener(Event.ENTER_FRAME,onFrame); 61 62 _Skin.addFrameScript(100,function():void 63 { 64 // 第一种调用图层写法 65 var myChildMc_0 : MovieClip = _Skin.getChildByName("myChildMc_0") as MovieClip; 66 // 添加点击事件 67 myChildMc_0.addEventListener(MouseEvent.CLICK,onClick); 68 myChildMc_0.buttonMode = true; 69 70 // 第二种调用图层写法 71 var myChildMc_1 : MovieClip = _Skin["myChildMc_1"] as MovieClip; 72 myChildMc_1.addEventListener(MouseEvent.CLICK,onClick); 73 myChildMc_1.buttonMode = true; 74 _Skin.stop(); 75 }) 76 77 addChild(_Skin); 78 79 } 80 81 82 /** 83 * 点击播放动画,并播放配音 84 * @param e 85 * 86 */ 87 public function onClick(e:MouseEvent):void 88 { 89 var myclickMc:MovieClip = e.currentTarget as MovieClip; 90 var soundnum:int = myclickMc.name.split(‘_‘)[1]; 91 _audio.init(soundnum); 92 myclickMc.gotoAndPlay(2); 93 } 94 95 96 /** 97 *开场动画时,指定帧播放对应配音 98 * @param e 99 * 100 */ 101 public function onFrame(e:Event):void 102 { 103 if(_xmlList.FrameDict[count]) 104 { 105 _audio.init(_xmlList.FrameDict[count]); 106 } 107 count++; 108 } 109 110 } 111 }
配音控制类,VoicePlay:
1 package 2 { 3 import flash.display.Sprite; 4 import flash.media.Sound; 5 import flash.net.URLRequest; 6 7 8 /** 9 * 声音控制类 10 * @ add hunter 11 * 12 */ 13 public class VoicePlay extends Sprite 14 { 15 public var arr:Array = [‘audio/are you ok.mp3‘,‘audio/Yes, I am. I’m amazed.mp3‘]; 16 public function VoicePlay() 17 { 18 } 19 20 public function init(count:int):void 21 { 22 var snd:Sound = new Sound(new URLRequest(arr[count])); 23 snd.play(); 24 } 25 26 } 27 }
过场动画配音播放帧类,XmlProject:
1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 import flash.net.URLLoader; 6 import flash.net.URLRequest; 7 import flash.utils.Dictionary; 8 9 10 /** 11 * 播放序列帧对应的配音 12 * @author 13 * 14 */ 15 public class XmlProject extends Sprite 16 { 17 public var FrameDict:Dictionary = new Dictionary(); 18 public function XmlProject() 19 { 20 var xmlLoad:URLLoader = new URLLoader(new URLRequest("PlayList.xml")); 21 xmlLoad.addEventListener(Event.COMPLETE,XMLloaded); 22 } 23 24 // 在Dictionary存储数据,播放配音对应帧数,以及帧对应的配音编号 25 public function XMLloaded(e:Event):void 26 { 27 var myxml:XML = new XML(e.currentTarget.data); 28 trace(myxml); 29 for(var i:int=0;i<myxml.frame.length();i++) 30 { 31 trace("@n = " + myxml.frame[i][email protected] + ", event = " + myxml.frame[i][email protected]); 32 FrameDict[int(myxml.frame[i][email protected])]=myxml.frame[i][email protected]; 33 } 34 35 } 36 37 } 38 }
过场动画配音xml表,PlayList:
1 <play> 2 <frame n="43" event="0"></frame> 3 <frame n="86" event="1"></frame> 4 </play>
时间: 2024-11-05 22:33:16