-
Creator 版本:v2.2.1
-
目标平台: web
-
详细报错信息,包含调用堆栈:
TypeError: this is undefined
ClickCountMediator.ts:29:4
clickCallback ClickCountMediator.ts:29
emit callbacks-invoker.js:344
_doDispatchEvent CCNode.js:558
dispatchEvent CCNode.js:1987
_mouseUpHandler CCNode.js:472
_callback CCEventListener.js:354
_onListenerCallback CCEventManager.js:1023
_dispatchEventToListeners CCEventManager.js:627
dispatchEvent CCEventManager.js:1014
registerSystemEvent CCInputManager.js:477
ClickCountView.ts 源码:
const { ccclass, property } = cc._decorator;
@ccclassexport default class ClickCountView extends cc.Component { @property(cc.Label) label: cc.Label = null;
@property(cc.Button) button: cc.Button = null;
@property value: number = 0;
onLoad() { this.label = this.node.getChildByName("label").getComponent(cc.Label); this.button = this.node.getChildByName("add").getComponent(cc.Button); this.value = 0; }
start() {}
public setButtonCallback(callback: any): void { this.button.node.on(cc.Node.EventType.TOUCH_END, callback); }
public setNumber(count : number){ this.label.string = `count is : ${count}`; }}
ClickCountMediator.ts 源码:
export class ClickCountMediator extends puremvc.Mediator { private clickCountView: ClickCountView = null; private clickCount: number = 0;
constructor(mediatorName?: string, viewComponent?: any) { super(mediatorName, viewComponent);
this.clickCount = 0; if (viewComponent == null) { return; }
this.clickCountView = (viewComponent as cc.Node).addComponent( ClickCountView ); this.bindListener(); }
private bindListener(): void { this.clickCountView.setButtonCallback(this.clickCallback); }
private clickCallback() { this.clickCount += 10; this.sendNotification(CommandName.ClickCountCommand, this.clickCount); }
public listNotificationInterests() { return [MediatorName.MediatorClickCount]; }
public handleNotification(notification : puremvc.INotification){ switch(notification.getName()){ case MediatorName.MediatorClickCount:{ let clickCountRepository : ClickCountRepository = notification.getBody(); this.clickCountView.setNumber(clickCountRepository.count); break; } } }}
我是在 ClickCountMediator.ts 中调用 ClickCountView.ts 中的 button 绑定事件,但是在 ClickCountMediator.ts 的回调函数中,出现了错误。显示 TypeError: this is undefined,请问下,这个情况怎么处理?
ClickCountView.ts 源码:

ClickCountMediator.ts 源码:
论坛的编辑器有点不会用,见谅!

