如何构建我的类以避免使用静态函数
我有一个 Flash 应用程序,可以创建一种 powerpoint 演示文稿。
所有“幻灯片”都存储在可读取和处理的 XML 文件中。
我正在尝试使用此 XML 文件构建演示文稿。
目前,我的主类具有初始函数 main
和静态函数 processXML
main
使用名为 initDB 的函数启动我的 database
类。我的问题之一是 initDB 放弃处理,因为它依赖于事件侦听器。加载 XML 文件完成后,事件侦听器会在 main
上启动静态函数,以从此文件创建对象。
问题在于,由于事件侦听器继续处理(在不确定的时间后),因此函数不再由 main
类控制。
通常,在这种情况下,我会避免使用静态,因为我会通过在处理时使用返回值来控制主函数的处理 - 即返回一个值以将控制拉回到主函数的函数。调用者类。
现在,所有这些都产生了连锁反应,我无法使用 addChild 调用,或者实际上任何类似的调用,因为该函数是静态的。
如果您能抽出一些时间,我真的需要重新考虑我的文件的工作方式。
主类
public static var databaseXML:XML;
public var database:ContentDatabase = new ContentDatabase();
public function main()
{
database.initDB();
}
public static function processXML()
{
//Get First Slide
var introSlide:SlideLayout = new SlideLayout();
var allSlides:XMLList = main.databaseXML.children();
var introSlideXML:XML;
for each (var slide:XML in allSlides)
{
introSlideXML = slide;
break;
}
var theSlide:MovieClip = introSlide.createSlide(introSlideXML);
addChild(theSlide); //Fails, Obviously
}
ContentDatabase 类
private var xmlLoader:URLLoader;
public function initDB()
{
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
xmlLoader.load(new URLRequest("resources/slides.xml"));
}
private function onComplete(e:Event):void
{
try
{
main.databaseXML = new XML(e.target.data);
xmlLoader.removeEventListener(Event.COMPLETE, onComplete);
xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
main.processXML();
}
catch (err:Error)
{
trace('broke: ' + err.message);
}
}
private function onIOError(e:IOErrorEvent):void
{
trace('broke: ' + e.text);
}
我对如何构建这个项目以允许我进行这种交流的所有想法持开放态度。
理想情况下,我希望 ContentDatabase 类只包含 XML 处理。
I have a Flash application that creates a sort of powerpoint presentation.
All the 'slides' are stored in an XML files which is read and processed.
I'm trying to build the presentation using this XML file.
At the moment, my main class has it's initial function main
and a static function processXML
main
initiates my database
class with a function called initDB. One of my issues is that initDB forgoes processing because it's dependant on an event listener. On completion of loading the XML files, the event listener initiates my static function on my main
to create objects from this file.
The issue is that because the event listener continues the processing (after an indeterminate amount of time), the functions are no longer controlled by the main
class.
Normally, in this situation, I'd avoid the use of statics because I'd control processing from the main function by using returns
on processing - i.e. a function that returns a value to pull control back to the caller class.
Now, all this has had a knock on effect and I can't use addChild calls, or indeed any similar calls because the function is static.
If you could spare some time, I really need to re-think the way my files work.
Main Class
public static var databaseXML:XML;
public var database:ContentDatabase = new ContentDatabase();
public function main()
{
database.initDB();
}
public static function processXML()
{
//Get First Slide
var introSlide:SlideLayout = new SlideLayout();
var allSlides:XMLList = main.databaseXML.children();
var introSlideXML:XML;
for each (var slide:XML in allSlides)
{
introSlideXML = slide;
break;
}
var theSlide:MovieClip = introSlide.createSlide(introSlideXML);
addChild(theSlide); //Fails, Obviously
}
ContentDatabase Class
private var xmlLoader:URLLoader;
public function initDB()
{
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
xmlLoader.load(new URLRequest("resources/slides.xml"));
}
private function onComplete(e:Event):void
{
try
{
main.databaseXML = new XML(e.target.data);
xmlLoader.removeEventListener(Event.COMPLETE, onComplete);
xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
main.processXML();
}
catch (err:Error)
{
trace('broke: ' + err.message);
}
}
private function onIOError(e:IOErrorEvent):void
{
trace('broke: ' + e.text);
}
I'm open to all ideas about how I structure this project to allow me this kind of communication.
Ideally, I'd like the ContentDatabase
class to hold nothing but XML processing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的主要和ContentDatabase 类太紧密了!
您的数据包含在 XML 文件中,实际上您需要首先加载文件、解析、组织和加载文件。存储数据,然后您可以操作该数据。
我不同意里奇先生的观点,这里确实不需要单例。
ContentDatabase 应该只关心加载 XML ,理想情况下源 url 不应该在类中硬编码。
例如,您可以将 url 作为参数传递给 initDb 方法。
加载数据后,好的做法是调度一个事件来通知 Main 类数据已准备好。您可以使用 CustomEvent 或使用 信号并将数据作为对象传递。这样,Main 类和 ContentDatabase 类之间就不会有不必要的依赖关系。
然后在你的 Main 类中
有几种方法可以调度 &听主课和主课之间的事件内容数据库。如上所述,您可以使用信号。您还可以通过将事件调度程序传递到 ContentDatabase 来创建轻依赖项。这样做是为了确保同一个调度程序侦听并调度事件。
然后在 ContentDatabase 构造函数中...
然后在 processXML() 中
当然还有您的 Main 类
虽然可以使用 XML 作为本机 AS3 对象,但我通常更喜欢创建自己的对象或类,用 XML 填充它数据,以便我可以直接调用其属性,而不必查询 XML,但这是个人喜好......
Your Main & ContentDatabase classes are too tied up!
Your data is contained in an XML file, practically you need to first load the file, parse , organize & store the data, then you can manipulate this data.
I disagree with Sr.Richie, a Singleton is really not needed here.
ContentDatabase should simply care about loading the XML , ideally the source url shouldn't be hardcoded in the class.
For instance , you could pass the url as a parameter to the initDb method
After the data has been loaded, good practice would be to dispatch an event to inform the Main class that the data is ready. You could use a CustomEvent or use a Signal and pass the data as an Object. In this way you wouldn't have unnecessary dependencies between the Main and the ContentDatabase classes.
Then in your Main class
There are several ways to dispatch & listen to Events between the Main class & the ContentDatabase. As mentioned above , you could use Signals. You could also create a light dependency by passing an event dispatcher to the ContentDatabase. You do that to ensure that the same dispatcher listens to and dispatches event.
Then in the ContentDatabase contstructor...
then in processXML()
And of course in your Main class
Although it is possible to use an XML as a native AS3 object, I often prefer to create my own Object or Class, populate it with the XML data so that I can call its properties directly , instead of having to query the XML , but that's personal taste...