向嵌套组件添加功能
好的,情况是这样...
我有一个自定义 mxml 组件,其中包含几个图像和 4 个按钮。组件文件已包含每个按钮的 clickHandler
。我需要能够访问 clickHandler
或创建另一个函数并将其附加到我的 Main.mxml
文件中的这些按钮。我应该添加到原始的 clickHandlers
中吗?如果是这样,如何将该方法应用于我的 Main.mxml
文件?
仅供参考:该组件有 5 个状态,每个 clickHandler
都会触发状态之间的转换。
这是组件文件中的两个 clickHandlers
:
protected function submit_clickHandler():void
{
const state:String = currentState;
if ( state == 'state1' ) {
currentState='state2';
}
if ( state == 'state3' ) {
currentState='state4';
addElement(images[i]); //The methods I want to run from
getStudentAnswer(); //within the Main.mxml.
submit(); //If I add them here, I get an
newQuestion(); //undefined method error.
}
if ( state == 'state4' ) {
currentState='state4';
}
if ( state == 'state5' ) {
currentState='state4';
}
if ( state == 'state3' ) {
Sequence1.play();
}
}
protected function checkAnswer_clickHandler():void
{
const state:String = currentState;
if ( state == 'state2' ) {
currentState='state1';
}
if ( state == 'state4' ) {
currentState='state5';
}
}
谢谢, 杰明
O.K. Here's the situation...
I have a custom mxml component that contains a couple of images and 4 buttons. The component file already contains a clickHandler
for each of the buttons. I need to be able to either access the clickHandler
or create another function and attach it to those buttons from within my Main.mxml
file. Should I add to the original clickHandlers
? if so, how do I apply the method to my Main.mxml
file?
FYI: The component has 5 states and each clickHandler
triggers transitions between the states.
Here are the two clickHandlers
from the component file:
protected function submit_clickHandler():void
{
const state:String = currentState;
if ( state == 'state1' ) {
currentState='state2';
}
if ( state == 'state3' ) {
currentState='state4';
addElement(images[i]); //The methods I want to run from
getStudentAnswer(); //within the Main.mxml.
submit(); //If I add them here, I get an
newQuestion(); //undefined method error.
}
if ( state == 'state4' ) {
currentState='state4';
}
if ( state == 'state5' ) {
currentState='state4';
}
if ( state == 'state3' ) {
Sequence1.play();
}
}
protected function checkAnswer_clickHandler():void
{
const state:String = currentState;
if ( state == 'state2' ) {
currentState='state1';
}
if ( state == 'state4' ) {
currentState='state5';
}
}
Thanks,
JM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有权访问按钮的调度事件,只需将“bubbles”设置为“true”并侦听 Main.mxml 中的事件。如果您将显示列表视为具有分支的层次树,则冒泡的事件将沿分支向上移动,直到到达第一个 DisplayObject。在此过程中,它会检查每个 DisplayObject 以查找它是否正在等待捕获事件。
下面是一个基本示例,其中 Main 正在侦听单击事件,并且有一个子 MovieClip 实例和一个子 Sprite 实例。 Sprite 实例有一个正在调度单击事件的子 Button 实例。
Button 中分派的单击事件将是:
在 Main 中,您只需监听该事件:
因为 MouseEvent.CLICK 是一个常见事件,所以我建议在这种类型中创建一个自定义事件(或至少一个唯一的事件名称)场景以避免 DisplayObject 捕获错误事件的可能性。但是,希望这能让您更好地了解如何处理冒泡。
-
如果没有冒泡,捕获事件的唯一其他方法是使用点语法。我不推荐这种方法,因为它可能会变得混乱,但这有助于说明冒泡的概念(假设每个子 DisplayObject 都是公共的):
In Main:(再次,不推荐)
希望有所帮助!
[编辑 - 添加自定义事件]
自定义事件类如下所示:
然后您将调度一个事件,如下所示:
并侦听该事件,如下所示:
If you have access to the dispatch event for the buttons, just set "bubbles" to "true" and listen for the event in Main.mxml. If you think of the display list as a hierarchal tree with branches, an event that bubbles will travel up the branch until it reaches the first DisplayObject. Along the way, it checks each DisplayObject to find if it is waiting to capture the event.
Here is a basic illustration where Main is listening for a click event and has a child MovieClip instance with a child Sprite instance. The Sprite instance has a child Button instance that is dispatching a click event.
The dispatched click event in Button would be:
In Main, you would just listend for the event:
Because MouseEvent.CLICK is such a common event, I would suggest creating a custom event (or at least a unique event name) in this type of scenario to avoid the possibility of DisplayObject's capturing incorrect events. But, hopefully this gives a better idea of how to handle bubbling.
-
Without bubbling, the only other way to capture the event would be to use dot syntax. I don't recommend this method because it could get messy, but this helps to illustrate the concept of bubbling (assuming each child DisplayObject is public):
In Main: (again, not recommended)
Hope that helps!
[EDIT - Added custom Event]
Here is what a custom Event class would look like:
You would then dispatch an event like:
And listen for the event like: