javascript - 如何让我的文字跟随我在 Phaser 3 中的玩家?
问题描述
我正在使用 Phaser 3 框架创建游戏。我的游戏使用滚动摄像头,所以根据我的搜索,显示分数的最简单方法是使用容器。我正在尝试使用补间来确保它跟随玩家,但我似乎无法找出正确的属性。
我希望得分直接从玩家上方开始,并且随着他移动而移动。
如果有比使用容器更好的方法,请随意更改它。
//Camera to follow the skater
this.cameras.main.setBounds(0, 0, 3000, gameHeight);
this.cameras.main.startFollow(skater);
// ...some code in between...
//Scoreboard
scoreBoard = this.add.container(skater.x, 50);
scoreText = this.add.text(skater.x, 50, "SCORE: 0", {fontSize: '56px', color: '#fff'});
scoreBoard.add(scoreText);
this.tweens.add({
targets: scoreBoard,
x: scoreBoard.x + skater.x,
ease: 'Linear',
duration: 1,
delay: 1,
yoyo: false,
repeat: -1
});
注意:所有这些代码只在create()
函数中。
解决方案
解决方案非常简单。在update()
函数中,将scoreText
变量设置为skater.body.position.x
:
function update() {
scoreText.x = skater.body.position.x;
}
推荐阅读
- python-3.x - 如何在使用 boto3 创建 api-gateway 时添加命令“使用 Lambda 代理集成”
- java - 在测试中调用 SDOHelper.createRootDataObject 时的 NPE
- spring - 从之前的步骤获取xml配置spring批处理中的jobExecutionContext
- android - 如何在Android中将手机上的图像文件作为LiveData返回?
- java - 为什么我的代码不能返回我想要的?
- php - 如何在 laravel 中获得不同的查询结果?
- javascript - 为什么我的递归循环最后两次打印最后一个值?
- javascript - 在元素 Vue vm 中包含脚本标签绑定到抛出模板错误
- ruby-on-rails - button_to 没有打开确认对话框
- javascript - 递归获取返回一个承诺数组而不是值