Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.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
如何在Phaser(HTML)中生成和更新文本_Html_Phaser Framework - Fatal编程技术网

如何在Phaser(HTML)中生成和更新文本

如何在Phaser(HTML)中生成和更新文本,html,phaser-framework,Html,Phaser Framework,我在create函数中创建了一个变量,通过game.add.text(…)显示一个数字,但我希望每次按下按钮时文本都会更新,因为变量会发生变化。如果我在更新函数中执行game.add.text(…),它会堆叠起来,并出现许多层文本。尝试以下操作: 单击BTN:number=0;//比如说 game.add.text('Clicks'+this.clickBtn) this.btn=this.add.button(一些代码,this.yourFunction) 这是你的函数(){ 点击此按钮。点击

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

尝试以下操作:

单击BTN:number=0;//比如说

game.add.text('Clicks'+this.clickBtn)

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

这是你的函数(){

点击此按钮。点击BTN+=1

}

我想这会对你有帮助


你好,谢尔盖。

这里有一个简单的版本,可以让你知道如何处理这个问题

<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>

var game=new Phaser.game(400300,Phaser.AUTO,'Phaser-example',{preload:preload,create:create});
//您需要确保您的按钮单击操作可以访问要显示的号码。
var-clickCount=0;
//我们还需要跟踪添加到游戏中的文本对象。
var点击计数器;
函数预加载(){
game.load.image('button','assets/button.png');
}
函数create(){
game.stage.backgroundColor='#124184';
//创建文本对象时,请确保可以从按钮处理程序访问它。
clickCounter=game.add.text(5,5,'总点击数:'+clickCount,{fill:'#ffffff',字体:'14pt Arial'});
game.add.button(200150,'按钮',actionClick,这个);
}
函数actionClick(){
点击计数++;
//这里的键是setText(),它允许您更新文本对象的文本。
clickCounter.setText('点击总数:'+点击次数);
}

因此,与其创建新的文本对象,不如存储对您创建的文本对象的引用,并使用
setText()
对其进行更新。

谢谢!虽然我有一个小问题,但到目前为止效果很好。我不能给它添加我自己的图像。我将“assets/button.png”替换为按钮图像命名的“button.png”。但实际游戏中没有图像,按钮也会消失。但是当我对一些不存在的东西做“button.png”时,按钮显示为绿色的正方形。有什么方法可以给按钮做造型吗?在不添加实际图像的情况下,如果查看浏览器中的“网络”选项卡并刷新,加载图像时是否会显示404?是的,您可以手动绘制背景,或者使用文本元素并设置其样式。