GameComponent添加批量绑定Button触摸事件功能

This commit is contained in:
donggang
2024-03-12 09:37:29 +08:00
parent 1f1a863734
commit 4edb3b0d6c
2 changed files with 15 additions and 2 deletions

View File

@@ -74,7 +74,7 @@ export class TimerManager extends Component {
}
}
*/
register(object: any, field: string, onSecond: Function, onComplete: Function): any {
register(object: any, field: string, onSecond: Function, onComplete: Function): string {
var timer = new Timer();
timer.step = 1;

View File

@@ -4,7 +4,7 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-12-13 11:36:00
*/
import { Asset, Component, EventKeyboard, EventTouch, Input, Node, __private, _decorator, input } from "cc";
import { Asset, Button, Component, EventKeyboard, EventTouch, Input, Node, __private, _decorator, input } from "cc";
import { oops } from "../../core/Oops";
import { EventDispatcher } from "../../core/common/event/EventDispatcher";
import { EventMessage, ListenerFunc } from "../../core/common/event/EventMessage";
@@ -233,6 +233,7 @@ export class GameComponent extends Component {
this.setButtonList(args);
}
protected setButtonList(args: string[]) {
// 自定义按钮批量绑定触摸事件
this.node.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
var self: any = this;
var func = self[event.target.name];
@@ -241,6 +242,18 @@ export class GameComponent extends Component {
else
console.error(`名为【${event.target.name}】的按钮事件方法不存在`);
}, this);
// Cocos Creator Button组件批量绑定触摸事件
var buttons = this.node.getComponentsInChildren<Button>(Button);
buttons.forEach((b: Button) => {
var node = b.node;
var self: any = this;
var func = self[node.name];
if (func)
node.on(Node.EventType.TOUCH_END, func, this);
else
console.error(`名为【${node.name}】的按钮事件方法不存在`);
});
}
/**