我在 PhaserJS 3 游戏中显示了一个文本,然后 onEvent 方法在循环中运行,向当前文本添加新文本,它会一直工作,直到找到 ' ',但它根本没有给出任何错误。
function create() {
this.savedText = this.add.text(0, 100, "Starting text display")
this.newText = "This text should be displayed \n in two lines"
this.index = 0
this.timedEvent = this.time.addEvent({ delay: 100, callback: this.onEvent, callbackScope: this, loop: true })
}
onEvent() {
let textDisplayed = ""
if(this.index < parseInt(this.newText.length)) {
textDisplayed += this.newText.charAt(this.index)
this.savedText.text += textDisplayed
this.index++
}
}
我该如何解决这个问题?有什么提示吗?预先感谢。
如果您发布的代码 100% 相同,那么您只是错过了
function
之前的关键字 onEvent()
。因为这段代码有效
演示:
document.body.style = 'margin:0;';
var config = {
type: Phaser.AUTO,
width: 536,
height: 183,
scene: {
create
},
};
function create() {
this.savedText = this.add.text(0, 100, "Starting text display ")
this.newText = "This text should be displayed \n in two lines"
this.index = 0
this.timedEvent = this.time.addEvent({ delay: 100, callback: onEvent, callbackScope: this, loop: true })
}
function onEvent() {
let textDisplayed = ""
if(this.index < parseInt(this.newText.length)) {
textDisplayed += this.newText.charAt(this.index)
this.savedText.text += textDisplayed
this.index++
}
}
new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>