自定义事件监听器 Flash AS3
不确定这是否是最好的选择,但想也许我可以使用事件监听器来节省一些时间。我每秒加载一次 XML 文件,并且希望如果 XML 中存在特定值,则有触发器,到目前为止我有这样的:
//TIMER THAT LOADS XML ONCE A SECOND
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
var myTimer:Timer = new Timer(100,2); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
function runOnce(event:TimerEvent):void {
trace("HELLO");
myLoader.load(new URLRequest("myxml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
}
function processXML(e:Event):void {
trace("load XML");
myXML = new XML(e.target.data);
trace(myXML);
}
现在我希望事件监听器在“方向”等于“左”或“右”时执行某些操作:
addEventListener(myXML.LEFT, movieMC_Left);
addEventListener(myXML.LEFT, movieMC_Right);
//movieMC_Left and movieMC_Right functions that do something.
Not sure if this is the best option but thought maybe if I could use an eventlistener that would save some time. I am loading an XML file once a second and want there to be triggers if specific values are in the XML so far I have this:
//TIMER THAT LOADS XML ONCE A SECOND
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
var myTimer:Timer = new Timer(100,2); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
function runOnce(event:TimerEvent):void {
trace("HELLO");
myLoader.load(new URLRequest("myxml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
}
function processXML(e:Event):void {
trace("load XML");
myXML = new XML(e.target.data);
trace(myXML);
}
Now I want event listners that do something if "direction" is equal to Left or Right:
addEventListener(myXML.LEFT, movieMC_Left);
addEventListener(myXML.LEFT, movieMC_Right);
//movieMC_Left and movieMC_Right functions that do something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不久前发表了一篇关于实现自定义事件的帖子。它可能对你有用。我有点明白你在这里做什么 - 但你错过了一个关键点,即事件的调度:
这假设你已经创建了一个扩展名为 XMLEvent 的事件的类(如果你对此有疑问,请阅读我的文章),并且它包含静态常量 LEFT 和 RIGHT。它还假设您正在派发事件的类继承自 EventDispatcher(如果它是精灵或影片剪辑,则它会继承)。
否则=你的设置应该可以工作。
你到底在做什么?通过 XML 驱动动画?嗯。您的逻辑可能存在缺陷 - 如果您的加载时间超过一秒(根据您的时间),您将开始抛出错误。
无论如何——干杯
I made a post a little while ago about implementing custom events. It may prove useful to you. I kind of get what you are doing here - but you are missing a key point, the dispatching of an event:
This assumes you've created a class that extends Event called XMLEvent (read my post if you have questions about that), and it contains the static const LEFT and RIGHT. It also assumes the class you are dispatching events from inherits from EventDispatcher (which it does if its a sprite or movieclip).
Otherwise = your setup ought to work.
What are you doing, exactly? Driving animation via XML? hmmm. Your logic is potentially flawed in that - if your load takes longer than a second (based on your time) you will start throwing errors.
Anyway - cheers
事件类型只是字符串。因此,要监听两个不同的事件,您可以这样做:
为了进行额外级别的类型检查,您可以使事件类型保持不变。
因此,在您的流程 XML 函数中,您将循环遍历 XML 对象来查找方向变量。当您找到您要查找的内容时,调度一个事件:
最后一件事:确保使侦听器函数 onEventRightCalled 和 onEventLeftCalled。
Event types are just strings. So to listen for two different events you can do this:
For an extra level of type checking you can make your event types contants.
So in your process XML function, you will loop thru you XML object looking for the direction variable. When you find what you are looking for, dispatch an event:
One last thing: Be sure to make the listener functions, onEventRightCalled, and onEventLeftCalled.