[cocosCreator,cocos2d-x]无法使用bind for loadScene回调函数?

问题描述 投票:0回答:1

如果在loadScene()回调函数上使用bind,则getScene()信息不正确。

从Entry Scene切换到Lobby Scene后,我将进行后期处理。

它是使用typeScript实现的。

Entry.ts
cc.director.loadScene('Lobby', this.postProcessLobby.bind(this)(isRelogin));

enter image description here

控制台窗口中cc.director.getScene()的值表示'Entry''Lobby'是正确的,因为它在切换场景后被调用。

另一个例子是使用箭头功能来获得我想要的结果。

Entry.ts
cc.director.loadScene('Lobby', e => this.postProcessLobby(isRelogin));

enter image description here

cc.director.getScene()中的值表示“Lobby”,并且lobby变量的值显示为normal。

我更喜欢bind()用于异步回调处理,并且写得很好而没有问题。但是,loadScene()没有得到所需的结果。这到底是什么?场景回叫没有切换?还是有另一个问题?

typescript cocos2d-js cocoscreator
1个回答
0
投票

您的第一个代码段使用参数this.postProcessLobby.bind(this)调用isRelogin,并将返回值作为第二个参数传递给loadScene。那可能不是你的意思。你可以使用this.postProcessLobby.bind(this, isRelogin)生成一个回调,它将isRelogin添加到它被调用的参数列表之前,这应该适用于你的情况,因为它看起来像你的postProcessLobby实现忽略额外的参数,但箭头函数似乎是明确的解决方案我。

© www.soinside.com 2019 - 2024. All rights reserved.