js侦听事件未定义
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class.
我遇到了很多有关如何从动画片段访问文档类中的内容或如何从文档类中的影片剪辑中访问某些内容的问题。
It took me a while to figure this out but once I did it makes life so much easier.
我花了一些时间才弄清楚这一点,但是一旦我做到了,生活就会变得更加轻松。
By creating a CustomEvent class and placing it in your own utils folder and setting up your AS3 class path, once you import the Custom Event, this is how you'd go about it.
通过创建CustomEvent类并将其放置在您自己的utils文件夹中并设置AS3类路径,一旦导入Custom Event,就可以使用该方法。
your class would look something like this ...
你的班级看起来像这样...
package whatever.utils //change your package name as required
打包what.utils //根据需要更改包名
{
{
import flash.events.Event;
导入flash.events.Event;
public class CustomEvent extends Event
公共类CustomEvent扩展Event
{
{
public static const NOTIFY:String = "notify";
公共静态const NOTIFY:String =“ notify”;
public var data:*;
公共变量数据:*;
public function CustomEvent(type:String, data:*, bubbles:Boolean = false, cancelable:Boolean = false){
公共函数CustomEvent(type:String,data:*,Bubbles:Boolean = false,cancelable:Boolean = false){
super(type, bubbles, cancelable);
超级(类型,气泡,可取消);
this.data = data;
this.data =数据;
}
}
}
}
}
}
to implement
实施
say you have a nested movieclip that when it hits frame 5 you want to execute a function in the document class called callSomething();
假设您有一个嵌套的动画片段,当它到达第5帧时,您想在文档类中执行一个称为callSomething()的函数;
when you add the movieclip in your document class
当您在文档类中添加动画片段时
addChild(nameOfMovieClip);
addChild(nameOfMovieClip);
//add the dispatcher
//添加调度程序
nameOfMovieClip.addEventLi
nameOfMovieClip.addEventLi Stener(Cus tomEvent.N OTIFY,handleNotifyRoot);
//note the CustomEvent.NOTIFY part - you can add items in your CustomEvent class
//注意CustomEvent.NOTIFY部分-您可以在CustomEvent类中添加项目
Create a handleNotifyRoot method in your document class
在您的文档类中创建一个handleNotifyRoot方法
function handleNotifyRoot(e:CustomE
函数handleNotifyRoot(e:CustomE 发泄):无效
{
{
trace(e.data.myData.toStri
跟踪(e.data.myData.toStri ng());
}
}
in the movieclip when you want the event to trigger, you must dispatch the event
在动画片段中,当您要触发事件时,必须调度事件
var varString:String = "clicked ball";
var varString:String =“被点击的球”;
dispatchEvent(new CustomEvent(CustomEvent.NO
dispatchEvent(new CustomEvent(CustomEvent.NO TIFY,{myData:varString}));
you can pass anything to your custom event.
您可以将任何内容传递给自定义事件。
I hope this all makes sense.
我希望这一切都有意义。
翻译自: https://www.experts-exchange.com/articles/2458/Creating-a-custom-event-listener-in-AS3.html
js侦听事件未定义