自定义事件监听器 Flash AS3

发布于 2024-11-08 11:51:06 字数 869 浏览 0 评论 0原文

不确定这是否是最好的选择,但想也许我可以使用事件监听器来节省一些时间。我每秒加载一次 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-11-15 11:51:06

我不久前发表了一篇关于实现自定义事件的帖子。它可能对你有用。我有点明白你在这里做什么 - 但你错过了一个关键点,即事件的调度:

function processXML(e:Event):void {
    myXML = new XML(e.target.data);

    if (myXML.direction == "Left")
    {
       this.dispatchEvent(new XMLEvent(new XMLEvent.LEFT));
    }
    else
    {
    this.dispatchEvent(new XMLEvent(new XMLEvent.RIGHT));
    }
}

这假设你已经创建了一个扩展名为 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:

function processXML(e:Event):void {
    myXML = new XML(e.target.data);

    if (myXML.direction == "Left")
    {
       this.dispatchEvent(new XMLEvent(new XMLEvent.LEFT));
    }
    else
    {
    this.dispatchEvent(new XMLEvent(new XMLEvent.RIGHT));
    }
}

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

拔了角的鹿 2024-11-15 11:51:06

事件类型只是字符串。因此,要监听两个不同的事件,您可以这样做:

addEventListener("XML left", onEventRightCalled);
addEventListener("XML Right, onEventLeftCalled);

为了进行额外级别的类型检查,您可以使事件类型保持不变。

public static const XMLLEFT:String = "XML left";
public static const XMLRIGHT:String = "XML right";
addEventListener(XMLLEFT, onEventRightCalled);
addEventListener(XMLRIGHT, onEventLeftCalled);

因此,在您的流程 XML 函数中,您将循环遍历 XML 对象来查找方向变量。当您找到您要查找的内容时,调度一个事件:

this.dispatchEvent(new Event(XMLLEFT, true));

最后一件事:确保使侦听器函数 onEventRightCalled 和 onEventLeftCalled。

Event types are just strings. So to listen for two different events you can do this:

addEventListener("XML left", onEventRightCalled);
addEventListener("XML Right, onEventLeftCalled);

For an extra level of type checking you can make your event types contants.

public static const XMLLEFT:String = "XML left";
public static const XMLRIGHT:String = "XML right";
addEventListener(XMLLEFT, onEventRightCalled);
addEventListener(XMLRIGHT, onEventLeftCalled);

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:

this.dispatchEvent(new Event(XMLLEFT, true));

One last thing: Be sure to make the listener functions, onEventRightCalled, and onEventLeftCalled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文