如何让我的文本跟随 Phaser 3 中的玩家?
How to get my text to follow my player in 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;
}
我正在使用 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;
}