Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EventHandler

“EventHandler” 类用来设置场景中的事件回调,该类允许用户设置回调目标节点,目标组件名,组件方法名,并可通过 emit 方法调用目标函数。

example
// Let's say we have a MainMenu component on newTarget
// file: MainMenu.ts
@ccclass('MainMenu')
export class MainMenu extends Component {
    // sender: the node MainMenu.ts belongs to
    // eventType: CustomEventData
    onClick (sender, eventType) {
        cc.log('click');
    }
}
import { Component } from 'cc';
const eventHandler = new Component.EventHandler();
eventHandler.target = newTarget;
eventHandler.component = "MainMenu";
eventHandler.handler = "OnClick";
eventHandler.customEventData = "my data";

Hierarchy

  • EventHandler

Index

Properties

_componentId

_componentId: string = ""

component

component: string = ""

事件响应函数所在组件名(脚本名), 比如例子中的脚本名 'MainMenu'

customEventData

customEventData: string = ""

自定义事件数据

handler

handler: string = ""

响应事件函数名,比如例子中的 'onClick' 方法名

target

target: Node | null = null

事件响应组件和函数所在节点

Accessors

componentName

  • get componentName(): any
  • set componentName(value: any): void

Methods

Private compId2Name

  • compId2Name(compId: any): any

Private compName2Id

  • compName2Id(compName: any): any

Private genCompIdIfNeeded

  • _genCompIdIfNeeded(): void

emit

  • emit(params: any[]): void
  • 触发目标组件上的指定 handler 函数,可以选择传递参数。

    example
    import { Component } from 'cc';
    const eventHandler = new Component.EventHandler();
    eventHandler.target = newTarget;
    eventHandler.component = "MainMenu";
    eventHandler.handler = "OnClick"
    eventHandler.emit(["param1", "param2", ....]);

    Parameters

    • params: any[]

      The arguments for invoking the callback

    Returns void

Static emitEvents

Generated using TypeDoc