marvster Posted June 21, 2014 Share Posted June 21, 2014 I'd used game.time.events.repeat() to let a line of dialog come on screen char by char and if the user presses the action button I will force the user to see the full line and only after an additional click the next line should be shown. Now I wonder how to stop the timer as it will run even if I force the end of the line, as it will run into an error.The code below shows up a TypeError and I can't figure out why... Any advice here? I've also tried *.stop(true), but it will also throw a TypeError.Dialog.prototype.nextLine = function () { if (this.isEndOfLine) { this.currentLine++; this.isEndOfLine = false; if (this.currentLine < this.dialogLines.length) { this.speaker.text = this.dialogLines[this.currentLine].speaker; this.spokenText.text = ''; this.timer = this.game.time.events.repeat(this.dialogSpeed, this.dialogLines[this.currentLine].speakerText.length + 1, this.updateLine, this); } else { this.closeDialog(); } } else { this.timer.destroy(); this.forceLineEnd(); }}; Link to comment Share on other sites More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 You need to remove it from the main Phaser.Timer object (an instance of which is accessible via game.time) as that's where the timer events are managed, like this:game.time.remove(this.timer);It seems a little unintuitive to do it this way but that's how it's done. Link to comment Share on other sites More sharing options...
marvster Posted June 21, 2014 Author Share Posted June 21, 2014 Thanks alot! That brought the solution:this.game.time.events.remove(this.timer);Totally wonder about the use of destroy() in this context, as it will not be in use(?)... Just for those, who were curios about dialogs and how to manage them in phaser, here's the full class (yet) with my thoughts on it.May it help someone someday: 'use strict';var Dialog = function (game, x, y, frame) { Phaser.Sprite.call(this, game, x, y, 'dialog', frame); this.fixedToCamera = true; this.dialogType = 'default'; this.dialogSpeed = 80; this.fontStyle = { font: "18px Arial", fill: "#FFFFFF" }; this.isActive = false; this.isEndOfLine = true; this.visible = false; this.dialogLines = []; // {speaker:'name of speaker',speakerText:'text spoken'} this.speaker = this.game.add.text(15, 10, '', this.fontStyle ); this.spokenText = this.game.add.text(15, 30, '', this.fontStyle); this.addChild(this.speaker); this.addChild(this.spokenText);};Dialog.prototype = Object.create(Phaser.Sprite.prototype);Dialog.prototype.constructor = Dialog;Dialog.prototype.update = function () { if (this.isActive) { console.log(this.currentLine); } // write your prefab's specific update code here};Dialog.prototype.openDialog = function (dialogLines, dialogType) { //this.dialogLines = dialogLines; if (!this.isActive) { this.isActive = true; this.dialogType = dialogType ? dialogType : 'default'; this.dialogLines = dialogLines ? dialogLines : [{speaker:'...',spokenText: '...'}]; this.currentLine = -1; this.speaker.text = ''; this.spokenText.text = ''; this.visible = true; this.nextLine(); }};Dialog.prototype.closeDialog = function() { this.isActive = false; this.visible = false; this.dirty = true; this.isEndOfLine = true;};Dialog.prototype.forceLineEnd = function() { this.spokenText.text = this.dialogLines[this.currentLine].speakerText; this.isEndOfLine = true;};Dialog.prototype.nextLine = function () { if (this.isEndOfLine) { this.currentLine++; this.isEndOfLine = false; if (this.currentLine < this.dialogLines.length) { this.speaker.text = this.dialogLines[this.currentLine].speaker; this.spokenText.text = ''; this.timer = this.game.time.events.repeat(this.dialogSpeed, this.dialogLines[this.currentLine].speakerText.length + 1, this.updateLine, this); } else { this.closeDialog(); } } else { this.game.time.events.remove(this.timer); this.forceLineEnd(); }};Dialog.prototype.updateLine = function () { if (this.currentLine < this.dialogLines.length && this.spokenText.text.length < this.dialogLines[this.currentLine].speakerText.length) { this.spokenText.text = this.dialogLines[this.currentLine].speakerText.substr(0, this.spokenText.text.length + 1 ); } else { this.isEndOfLine = true; }};module.exports = Dialog;*the idea for dialogType is for future planning, to have some dialogs which come only within the box and some which were promoted with a mug shot left and right on to the box (for scenes for example). Link to comment Share on other sites More sharing options...
Recommended Posts