javascript - 有没有办法从另一个脚本访问异步函数内部的函数?
问题描述
在 spark AR 中,我有多个脚本:一个 UIManager 脚本和一个 GameManager 脚本。我的 UI 管理器的异步函数中有一个游戏结束函数,但我想通过 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 */)
})
推荐阅读
- javascript - 使用Javascript隐藏基于正文的元素类
- mysql - 使用 Laravel DB::select 和 DB:row 使用 MySql 服务器功能
- uvm - 我可以在虚拟音序器上调用 start_item
- ajax - Ajax 的加载时间因调用脚本而异?
- tensorflow - 如何使用 Pycharm 内置服务器启动 tensorboard?
- search - HAPI FHIR 自定义搜索响应
- java - 我如何调用这个 Ffun() 并访问这个类?
- python - 使用 Beautiful Soup 从可点击链接下载 CSV
- fortran - 如何从彩色波长转换为 RGB 或 HSL?
- gpu - 将数据从计算管道的输出资源传输到 DirectX 12 中的交换链图像?