首页 > 解决方案 > 如何让我的文字跟随我在 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()函数中。

标签: javascriptphaser-framework

解决方案


解决方案非常简单。在update()函数中,将scoreText变量设置为skater.body.position.x

function update() {
    scoreText.x = skater.body.position.x;  
}

推荐阅读