Add: stop watch
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
function StopWatch() {
|
||||
this.ellpasedTime = 0;
|
||||
this.startTime = 0;
|
||||
|
||||
var fontStyle = StopWatch.DEFAULT_TEXT_FONT;
|
||||
|
||||
fontStyle.align = "right";
|
||||
fontStyle.boundsAlignH = "right";
|
||||
this.timerText = game.add.text(game.world.width - 200, 35, "", fontStyle);
|
||||
this.timerText.anchor.set(0, 0.5);
|
||||
|
||||
// var grd = this.label.context.createLinearGradient(0, 0, 0, StopWatch.FONT_HEIGHT_PX);
|
||||
// grd.addColorStop(0, '#8ED6FF');
|
||||
// grd.addColorStop(1, '#004CB3');
|
||||
// this.label.fill = grd;
|
||||
// this.scoreText.fill = grd;
|
||||
|
||||
// this.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
|
||||
// this.label.stroke = '#000';
|
||||
// this.label.strokeThickness = 3;
|
||||
|
||||
this.timerEvent = null;
|
||||
};
|
||||
|
||||
StopWatch.prototype.start = function() {
|
||||
this.startTime = game.time.totalElapsedSeconds();
|
||||
console.log(this.startTime);
|
||||
this.printTime();
|
||||
|
||||
if(this.timerEvent === null)
|
||||
this.timerEvent = game.time.events.loop(Phaser.Timer.SECOND / 10, this.updateTimer, this);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if(this.timerEvent)
|
||||
this.removeTimer();
|
||||
|
||||
return recordTime;
|
||||
}
|
||||
|
||||
StopWatch.prototype.updateTimer = function() {
|
||||
if(this.isPaused == true)
|
||||
return;
|
||||
|
||||
this.ellpasedTime = game.time.totalElapsedSeconds() - this.startTime;
|
||||
this.printTime();
|
||||
}
|
||||
|
||||
StopWatch.prototype.printTime = function() {
|
||||
var ellapsedTimeText = this.ellpasedTime.toString();
|
||||
var timeText = "";
|
||||
if(ellapsedTimeText.length == 1)
|
||||
timeText = ellapsedTimeText + ".0";
|
||||
else
|
||||
timeText = ellapsedTimeText.substring(0, 3);
|
||||
this.timerText.text = timeText + "초";
|
||||
}
|
||||
|
||||
StopWatch.prototype.removeTimer = function() {
|
||||
game.time.events.remove(this.timerEvent);
|
||||
this.timerEvent = 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"
|
||||
};
|
||||
Reference in New Issue
Block a user