js侦听事件未定义_在AS3中创建自定义事件侦听器

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.addEventListener(CustomEvent.NOTIFY, handleNotifyRoot);

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:CustomEvent):void

函数handleNotifyRoot(e:CustomE 发泄):无效

{

{

trace(e.data.myData.toString());

跟踪(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.NOTIFY, {myData:varString}));

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侦听事件未定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值