简体   繁体   English

EaselJS通过事件传递参数

[英]EaselJS passing parameters with events

I am using Adobe Flash CC to create EaselJS output manipulating the HTML5 canvas. 我正在使用Adobe Flash CC创建用于处理HTML5画布的EaselJS输出。 However there seems to be a massive oversight in the ability to pass parameters to event listeners. 但是,似乎很难对将参数传递给事件侦听器的能力进行监督。

The issue is that I offer multiple animation outputs for compatibility, one using EaselJS and one using Raphael , however the interface that controls these remains the same, these are plain HTML elements with data stored in attributes that I wish to call functions written in the Flash IDE by triggering events and passing parameters. 问题是我提供了多个动画输出以实现兼容性,一个使用EaselJS,另一个使用Raphael ,但是控制它们的接口保持不变,这些是纯HTML元素,数据存储在属性中,我希望调用用Flash编写的函数通过触发事件和传递参数来实现IDE。

I could easily find ways to avoid using EventDispatcher such as registering them with the root object and calling them directly with custom handlers. 我可以轻松地找到避免使用EventDispatcher方法,例如在根对象中注册它们并直接使用自定义处理程序调用它们。 However I would rather keep my Flash IDE output as universally compatible as possible and not pollute object's namespaces I have little control over. 但是,我宁愿使我的Flash IDE输出尽可能地通用兼容,而不污染我几乎无法控制的对象的名称空间。 I consider it a bad design pattern to write code in the Flash IDE that won't work without external aid. 我认为在Flash IDE中编写代码是一种糟糕的设计模式,如果没有外部帮助,该代码将无法正常工作。

Is there a way to pass parameters from an EaselJS event dispatcher to the listening events? 有没有办法将参数从EaselJS事件分配器传递到侦听事件? I know on() provides a data parameter but that is useless as I need to pass different parameters to the same event depending upon user interaction. 我知道on()提供了一个数据参数,但是这没用,因为我需要根据用户交互将不同的参数传递给同一事件。

You can put any properties on an Event object that you want. 您可以将任何属性放在所需的Event对象上。 When you dispatch an event, you can use a string like "complete" (which is better if you don't need parameters, since it won't generate an object if there are no listeners). 调度事件时,可以使用“ complete”之类的字符串(如果不需要参数,则更好,因为如果没有侦听器,它将不会生成对象)。 If you have parameters, create a new createjs.Event , and put whatever properties on it that you want: 如果您有参数,请创建一个新的createjs.Event ,并在其上添加所需的任何属性:

var event = new createjs.Event("complete");
event.time = new Date();
this.dispatchEvent(event);

Then you can inspect the event object in your handler. 然后,您可以在处理程序中检查事件对象。

myObject.addEventListener("complete", handler); // add a listener
function handler(event) {
    console.log(event.time);
}

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM