function StopWatch() { this.startTime = 0; this.ellapsedTime = 0; this.isPaused = true; this.timerEvent = null; var fontStyle = StopWatch.DEFAULT_TEXT_FONT; this.timerText = game.add.text(game.world.centerX, 35, "", fontStyle); this.timerText.anchor.set(0.5); this.printTime(); } StopWatch.prototype.start = function() { this.isPaused = false; this.startTime = game.time.totalElapsedSeconds(); this.ellapsedTimer = game.time.create(false); this.timerEvent = this.ellapsedTimer.loop(10, this.updateTimer, this); this.ellapsedTimer.start(); this.printTime(); } StopWatch.prototype.pause = function() { this.isPaused = true; } StopWatch.prototype.resume = function() { this.isPaused = false; } StopWatch.prototype.stop = function() { var recordTime = game.time.totalElapsedSeconds() - this.startTime; this.removeTimer(); return recordTime; } StopWatch.prototype.updateTimer = function() { if(this.isPaused == true) return; this.ellapsedTime = game.time.totalElapsedSeconds() - this.startTime; this.printTime(); } StopWatch.prototype.printTime = function() { this.timerText.text = RecordUtil.getRecordValueWithUnit( this.ellapsedTime, sessionStorageManager.getPlayingAppID() ); } StopWatch.prototype.removeTimer = function() { game.time.events.remove(this.timerEvent); if(this.timerEvent) this.timerEvent = null; this.ellapsedTimer.stop(); this.ellapsedTimer = null; } StopWatch.FONT_HEIGHT_PX = 70; StopWatch.DEFAULT_TEXT_FONT = { font: "38px Arial", align: "center", boundsAlignH: "center", // left, center. right boundsAlignV: "middle", // top, middle, bottom fill: "#fff" };