Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让我的文字在Phaser 3中跟随我的播放器?_Javascript_Phaser Framework - Fatal编程技术网

Javascript 如何让我的文字在Phaser 3中跟随我的播放器?

Javascript 如何让我的文字在Phaser 3中跟随我的播放器?,javascript,phaser-framework,Javascript,Phaser Framework,我正在用Phaser3框架创建一个游戏。我的游戏使用滚动摄像头,所以根据我搜索的内容,显示分数的最简单方法是使用一个容器。我试图使用tween来确保它跟随玩家,但我似乎无法找出正确的属性 我希望比分从球员正上方开始,并在球员移动的任何地方移动 如果有比使用容器更好的方法,请随意更改 //Camera to follow the skater this.cameras.main.setBounds(0, 0, 3000, gameHeight); this.cameras.main

我正在用Phaser3框架创建一个游戏。我的游戏使用滚动摄像头,所以根据我搜索的内容,显示分数的最简单方法是使用一个容器。我试图使用tween来确保它跟随玩家,但我似乎无法找出正确的属性

我希望比分从球员正上方开始,并在球员移动的任何地方移动

如果有比使用容器更好的方法,请随意更改

//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;  
}