繁体   English   中英

如何在 Phaser (HTML) 中制作和更新文本

[英]How can i make and update a text in Phaser (HTML)

我在 create 函数中创建了一个通过game.add.text(...)显示数字的变量,但我希望每次按下按钮时文本都会更新,因为变量会发生变化。 如果我在更新函数中执行game.add.text(...) ,它会被堆叠起来并且出现许多多层文本。

这是一个简单的版本,可以让您了解如何处理此问题。

<script>
    var game = new Phaser.Game(400, 300, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
    // You'll need to make sure your button click action can access the number to display.
    var clickCount = 0;
    // We'll also need to keep track of the text object that we add to the game.
    var clickCounter;

    function preload() {
        game.load.image('button', 'assets/button.png');
    }

    function create() {
        game.stage.backgroundColor = '#124184';
        // When creating the text object, make sure it'll be accessible from our button handler.
        clickCounter = game.add.text(5, 5, 'Total clicks: ' + clickCount, { fill: '#ffffff', font: '14pt Arial' });

        game.add.button(200, 150, 'button', actionOnClick, this);
    }

    function actionOnClick() {
        clickCount++;
        // The key here is setText(), which allows you to update the text of a text object.
        clickCounter.setText('Total clicks: ' + clickCount);
    }
</script>

因此,不要创建新的文本对象,而是存储对您创建的对象的引用并使用setText()更新它。

尝试这个:

clickBtn:number = 0; // 例如

game.add.text('点击次数' + this.clickBtn);

this.btn = this.add.button(一些代码,this.yourFunction);

this.yourFunction() {

this.clickBtn += 1;

}

我想它会帮助你。

问候,谢尔盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM