目前我主要使用的flash,ActionScript开发IDE为:Flex builder3/4,Flash CS3/4,FlashDevelop3.
一个游戏项目:如果不分模块开发,一个主程序会是相当的庞大,且不容易维护与多人并行开发。
分模块开发主要涉及到ActionScript面向接口开发。
一个程序员在经行一块开发的时候,应当将涉及的对象面向接口,而主程序只需要存在同样的接口,而不需要接口具体实现的类。虽然主程序与单块模块都同时存在这个接口,编译后的swf也许会大一点,但相对于其有点,这可以完全忽略。
将Flash编写的游戏会使用的元件等,编译生成.swf文件
接口:
package application.view.components.ui.chat.base { public interface IChat { function show(app:Object):void; function setMessage(kind:String,msg:String):void; } }
核心代码:
var loader:SWFLoader=new SWFLoader(); loader.Load("assets/ui/chat/mc_chat_world.swf"); loader.addEventListener(Event.COMPLETE, function(event:Event):void { // 获得非主程序中的其他类,通过swf获得 var ChatBig:Class=loader.GetClass("application.view.components.ui.chat.cls.ChatBig",loader.loadinfo); chatUI = new ChatBig() as IChat; chatUI.show(App.getApp()); App.getApp().chatFactory.bigChar = chatUI; } );
使用ChatBig的时候: (ChatBig as IChat).show(XXXXX);
--------------------------------------------------------------------------------------------------------------------------------------------------
Flex使用ActionScript Project开发游戏。
由于Flex的MXML中,addChild(mx.core.IUIComponent)无法使用MovieClip。故Flex开发游戏开发游戏是基于Sprite的。这是鄙人的错误,FlexMXML中使用Flash自定义组件方式,请参见后面!
将Flash编写的游戏会使用的元件等,编译生成.swc文件.再ActionScript Project中ActionScript Build Path-->Library Path-->Add SWC
直接使用.swc中的类。
附带常用的类SWFLoder
/* * SWF加载 */ package cn.vicky.as3.utils { import flash.display.Loader; import flash.display.LoaderInfo; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.SecurityErrorEvent; import flash.net.URLRequest; import flash.system.ApplicationDomain; import flash.system.LoaderContext; /** * SWF加载器 */ public class SWFLoader extends EventDispatcher { private var m_loadinfo:LoaderInfo; public function get loadinfo():LoaderInfo { return m_loadinfo } public function SWFLoader() {} /** 加载SWF */ public function Load(url:String):void { var loader:Loader = new Loader(); var context:LoaderContext = new LoaderContext(); /** 加载到子域 */ context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); InitLoadEvent(loader.contentLoaderInfo); loader.load(new URLRequest(url),context); } /** * 获取当前ApplicationDomain内的类定义 * * name类名称,必须包含完整的命名空间,如 Grave.Function.SWFLoader * info加载swf的LoadInfo,不指定则从当前域获取 * return获取的类定义,如果不存在返回null */ public function GetClass(name:String, info:LoaderInfo = null):Class { try { if (info == null) { return ApplicationDomain.currentDomain.getDefinition(name) as Class; } return info.applicationDomain.getDefinition(name) as Class; } catch (e:ReferenceError) { //trace("定义 " + name + " 不存在"); return null; } return null; } /** * @private * 监听加载事件 * * @param info加载对象的LoaderInfo */ private function InitLoadEvent(info : LoaderInfo):void { info.addEventListener(ProgressEvent.PROGRESS, this.onProgress); info.addEventListener(Event.COMPLETE, this.onComplete); info.addEventListener(IOErrorEvent.IO_ERROR, this.onError); info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onError); } /** * @private * 移除加载事件 * * @param inft加载对象的LoaderInfo */ private function RemoveLoadEvent(info:LoaderInfo):void { info.removeEventListener(Event.COMPLETE,onComplete); info.removeEventListener(ProgressEvent.PROGRESS,onProgress); info.removeEventListener(IOErrorEvent.IO_ERROR,onError); info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onError); } /** 加载事件 */ private function onComplete(e:Event):void { var info:LoaderInfo = e.currentTarget as LoaderInfo; RemoveLoadEvent(info); m_loadinfo = info; this.dispatchEvent(e); } /** 加载中 */ private function onProgress(e:ProgressEvent):void { this.dispatchEvent(e); } /** 出错事件 */ private function onError(e:Event):void { this.dispatchEvent(e); } } }
对比
[Embed(source="../Assets/NearHill1.swf")] var NearHill1:Class; [Embed(source="../Assets/NearHill2.swf")] var NearHill2:Class; var url1:String = "Assets/NearHill1.swf"; //var url2:String = "Assets/NearHill2.swf"; //var urlReq:URLRequest = new URLRequest(url1); //var urlReq2:URLRequest = new URLRequest(url2); //var loader:Loader = new Loader(); new NearHill1();
错误更正:
Flex使用MXML同样能加入使用Flash自定义的组建!
package cn.vicky.mco { import flash.display.MovieClip; public class Car extends MovieClip { public function Car() { super(); } } }
package cn.vicky.mco { import cn.vicky.inter.Animal; import flash.display.MovieClip; public class Person extends MovieClip implements Animal { public var username:String ; public function Person() { super(); } public function eat():void { trace(username + " can eat !"); } public function sleep():void { trace(username + " is sleeping !"); } } }
package cn.vicky.inter { public interface Animal { function eat():void; function sleep():void; } }
运行后,打印:
[SWF] G:/work2/Flex使用Flash自定义组建/bin-debug/Main.swf - 634,916 bytes after decompression
[SWF] G:/work2/Flex使用Flash自定义组建/bin-debug/Main.swf - 4,044 bytes after decompression
Vicky can eat !
Vicky is sleeping !