Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EventHandler

The EventHandler class sets the event callback in the scene. This class allows the user to set the callback target node, target component name, component method name, and call the target method through the emit method.

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 = ""

The name of the component(script) that contains target callback, such as the name 'MainMenu' of the script in the example

customEventData

customEventData: string = ""

Custom Event Data

handler

handler: string = ""

Event handler, such as the callback function name 'onClick' in the example

target

target: Node | null = null

The node that contains target component

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
  • Trigger the target callback with given arguments

    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