有没有办法从另一个脚本访问异步函数内部的函数



在spark AR中,我有多个脚本:一个UIManager脚本和一个GameManager脚本。我在UI管理器的异步函数中有一个game-over函数,但我想通过GameManager访问该函数。

我试过把导出放在游戏之前,但这是不允许的,我在网上找不到任何东西。有人知道这样做的方法吗?

代码:

(async function() 
{
//Function that gets called when the game is lost
export function GameOver(score = 0)
{
//Make the Game over screen visible
LoseScreen.hidden = false;
//Check if the gme has a shop
if(GameSettings.RequireShop)
{
//Make the Shop button visible
ShopButton.hidden = false;
}
else if(!GameSettings.RequireShop)
{
//Make the Shop button invisible
ShopButton.hidden = true;
}

//Set the value to the current screen
CurrentScreen = "LoseScreen";
//Make the game invisible
//NOTE: end the game here when making the final game
GameMenuObj.hidden = true;
//Score variable saved here and displayed on screen
Score = score;
TotalScore += Score;
LoseScore.text = Score.toString();
//Check if the game has a Leaderboard or shop
if(GameSettings.RequireleaderBoard || GameSettings.RequireShop)
{
//save your new score and money
SaveData();
}

//show / refresh money on screen
CoinText.text = TotalScore + "";
}
//Enables async/await in JS [part 2]
})();
(async function() {
// ...
function GameOver(score = 0) {
// ...
}
})

成为

/* ... all the variables and functions declared inside the async function
* to whom the GameOver needs access should now be passed as arguments
*/
export function GameOver(
/* ... arguments from the async function */,
score = 0
) {
// ...
}
(async function() {
// ...
GameOver(/* ... arguments */)
})

最新更新