Fix: timer countdown
This commit is contained in:
@@ -65,7 +65,6 @@ Chart.drawText = function(posX, posY, text, fontStyle) {
|
||||
|
||||
Chart.prototype.printContents = function() {
|
||||
if(isDebugMode()) {
|
||||
console.log("printContents : " + this.records.length);
|
||||
for(var i = 0; i < this.records.length; i++) {
|
||||
this.records[i].printDate("12/10");
|
||||
this.records[i].printScore(360);
|
||||
|
||||
+101
-33
@@ -11,9 +11,9 @@ function Timer() {
|
||||
var timerCenterY = 183;
|
||||
|
||||
this.timeHourText = this.makeTimerTextContent(timerCenterX - 240, timerCenterY, 160, "00");
|
||||
this.makeTimerTextContent(timerCenterX - 120, timerCenterY, 160, ":");
|
||||
this.timeHourMinuteSeperator = this.makeTimerTextContent(timerCenterX - 120, timerCenterY, 160, ":");
|
||||
this.timeMinuteText = this.makeTimerTextContent(timerCenterX, timerCenterY, 160, "00");
|
||||
this.makeTimerTextContent(timerCenterX + 110, timerCenterY + 20, 100, ":");
|
||||
this.timeMinuteSecondSeperator = this.makeTimerTextContent(timerCenterX + 110, timerCenterY + 20, 100, ":");
|
||||
this.timeSecondText = this.makeTimerTextContent(timerCenterX + 190, timerCenterY + 20, 100, "00");
|
||||
|
||||
// buttons
|
||||
@@ -28,34 +28,36 @@ function Timer() {
|
||||
|
||||
// hour button
|
||||
this.hourPlus10Button = this.makeTimePlusButton(hour10PosX, plusPosY, "▲",
|
||||
(function() { this.timePlus(10, "hour"); }).bind(this)
|
||||
(function() { this.timePlus(10, Timer.TYPE_HOUR); }).bind(this)
|
||||
);
|
||||
// this.hourPlus10Button.setInputEnabled(false);
|
||||
this.hourMinus10Button = this.makeTimeMinusButton(hour10PosX, minusPosY, "▼",
|
||||
(function() { this.timeMinus(-10, "hour"); }).bind(this)
|
||||
(function() { this.timeMinus(-10, Timer.TYPE_HOUR); }).bind(this)
|
||||
);
|
||||
|
||||
this.hourPlus1Button = this.makeTimePlusButton(hour1PosX, plusPosY, "▲",
|
||||
(function() { this.timePlus(1, "hour"); }).bind(this)
|
||||
(function() { this.timePlus(1, Timer.TYPE_HOUR); }).bind(this)
|
||||
);
|
||||
this.hourMinus1Button = this.makeTimeMinusButton(hour1PosX, minusPosY, "▼",
|
||||
(function() { this.timeMinus(-1, "hour"); }).bind(this)
|
||||
(function() { this.timeMinus(-1, Timer.TYPE_HOUR); }).bind(this)
|
||||
);
|
||||
|
||||
// minute button
|
||||
this.minutePlus10Button = this.makeTimePlusButton(minute10PosX, plusPosY, "▲",
|
||||
(function() { this.timePlus(10, "minute"); }).bind(this)
|
||||
(function() { this.timePlus(10, Timer.TYPE_MINUTE); }).bind(this)
|
||||
);
|
||||
this.minuteMinus10Button = this.makeTimeMinusButton(minute10PosX, minusPosY, "▼",
|
||||
(function() { this.timeMinus(-10, "minute"); }).bind(this)
|
||||
(function() { this.timeMinus(-10, Timer.TYPE_MINUTE); }).bind(this)
|
||||
);
|
||||
|
||||
this.minutePlus1Button = this.makeTimePlusButton(minute1PosX, plusPosY, "▲",
|
||||
(function() { this.timePlus(1, "minute"); }).bind(this)
|
||||
(function() { this.timePlus(1, Timer.TYPE_MINUTE); }).bind(this)
|
||||
);
|
||||
this.minuteMinus1Button = this.makeTimeMinusButton(minute1PosX, minusPosY, "▼",
|
||||
(function() { this.timeMinus(-1, "minute"); }).bind(this)
|
||||
(function() { this.timeMinus(-1, Timer.TYPE_MINUTE); }).bind(this)
|
||||
);
|
||||
|
||||
this.startTimer();
|
||||
}
|
||||
|
||||
|
||||
@@ -63,14 +65,72 @@ function Timer() {
|
||||
Timer.prototype.initVariables = function() {
|
||||
this.prevTime = null;
|
||||
|
||||
this.timeLeft = 0;
|
||||
this.timeLeftHour = 0;
|
||||
this.timeLeftMinute = 0;
|
||||
this.timeLeftSecond = 0;
|
||||
|
||||
this.timeLeft = Timer.TIME_DEFAULT_SEC;
|
||||
this.timerEvent = null;
|
||||
}
|
||||
|
||||
Timer.prototype.startTimer = function() {
|
||||
this.timeLeft = Timer.TIME_DEFAULT_SEC;
|
||||
this.updateTimeText();
|
||||
|
||||
this.timerEvent = game.time.events.loop(1000, this.tickTimeLeft, this);
|
||||
}
|
||||
|
||||
Timer.prototype.restartTimer = function() {
|
||||
// this.timeLeft = Timer.TIME_DEFAULT_SEC;
|
||||
// this.updateTimeText();
|
||||
|
||||
this.prevTime = this.getNowTime();
|
||||
|
||||
this.timerEvent = game.time.events.loop(1000, this.tickTimeLeft, this);
|
||||
}
|
||||
|
||||
Timer.prototype.tickTimeLeft = function() {
|
||||
var outFocusedTime = this.getOutFocusedTime();
|
||||
|
||||
if(outFocusedTime > 2)
|
||||
this.timeLeft -= outFocusedTime - 1;
|
||||
|
||||
this.timeLeft--;
|
||||
this.updateTimeText();
|
||||
|
||||
if(this.timeLeft < 0)
|
||||
this.timeOver();
|
||||
}
|
||||
|
||||
Timer.prototype.timeOver = function() {
|
||||
console.log("time over");
|
||||
// this.startButton.disable();
|
||||
// this.restartButton.disable();
|
||||
// this.pauseButton.disable();
|
||||
|
||||
// xhr = new XMLHttpRequest();
|
||||
// xhr.open('POST', 'delete_last_timestamp.php', true);
|
||||
// xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||||
// var param = 'UserID=' + playerUserID;
|
||||
// xhr.send(param);
|
||||
}
|
||||
|
||||
Timer.prototype.getNowTime = function() {
|
||||
var now = new Date();
|
||||
return now.getTime();
|
||||
}
|
||||
|
||||
|
||||
Timer.prototype.getOutFocusedTime = function() {
|
||||
var nowTime = this.getNowTime();
|
||||
|
||||
var diffTimeSec = 0;
|
||||
if(this.prevTime != null)
|
||||
var diffTimeSec = parseInt((nowTime - this.prevTime) / 1000);
|
||||
|
||||
this.prevTime = nowTime;
|
||||
|
||||
return diffTimeSec;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Timer.prototype.makeTimerTextContent = function(x, y, sizePx, content) {
|
||||
// style setting
|
||||
var timerTextStyle = { font: "bold 160px Arial", fill: "#fff", align: "right"};
|
||||
@@ -156,7 +216,7 @@ Timer.prototype.makeTimeButton = function(setting, buttonText, eventHandler) {
|
||||
Timer.prototype.timePlus = function(amount, type) {
|
||||
console.log(amount + type);
|
||||
|
||||
// if(type == "hour")
|
||||
// if(type == Timer.TYPE_HOUR)
|
||||
this.timeLeft += amount * 60;
|
||||
this.updateTimeText();
|
||||
this.sendTimeLeftToServer();
|
||||
@@ -165,7 +225,7 @@ Timer.prototype.timePlus = function(amount, type) {
|
||||
Timer.prototype.timeMinus = function(amount, type) {
|
||||
console.log(amount + type);
|
||||
|
||||
// if(type == "hour") {
|
||||
// if(type == Timer.TYPE_HOUR) {
|
||||
// var totalMinusMinute = amount * Timer.TIME_MINUTES_FOR_HOUR;
|
||||
// if(totalMinusMinute - )
|
||||
// }
|
||||
@@ -202,13 +262,16 @@ Timer.prototype.timeDown = function(sec) {
|
||||
// this.sendTimeLeftToServer();
|
||||
}
|
||||
|
||||
Timer.prototype.updateTimeVariables = function() {
|
||||
this.timeLeftHour = parseInt(this.timeLeft / (60 * 60));
|
||||
this.timeLeftMinute = parseInt((this.timeLeft % (60 * 60)) / 60);
|
||||
this.timeLeftSecond = this.timeLeft % 60;
|
||||
}
|
||||
|
||||
Timer.prototype.updateTimeText = function() {
|
||||
var secondsForHour = Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE;
|
||||
|
||||
var timeLeftHour = parseInt(this.timeLeft / secondsForHour);
|
||||
var timeLeftMinute = parseInt((this.timeLeft % secondsForHour) / Timer.TIME_SECONDS_FOR_MINUTE);
|
||||
var timeLeftSecond = this.timeLeft % Timer.TIME_SECONDS_FOR_MINUTE;
|
||||
|
||||
this.timeHourText.text = this.getTimeWithTimeFormat(timeLeftHour);
|
||||
this.timeMinuteText.text = this.getTimeWithTimeFormat(timeLeftMinute);
|
||||
this.timeSecondText.text = this.getTimeWithTimeFormat(timeLeftSecond);
|
||||
// this.updateTimeVariables();
|
||||
// this.timerText.text = this.getTimeWithTimeFormat();
|
||||
}
|
||||
@@ -224,15 +287,16 @@ Timer.prototype.getTwoDigitNumber = function(value) {
|
||||
return absValue;
|
||||
}
|
||||
|
||||
Timer.prototype.getTimeWithTimeFormat = function() {
|
||||
if(this.timeLeft > 0)
|
||||
return this.getTwoDigitNumber(this.timeLeftHour)
|
||||
+ ":" + this.getTwoDigitNumber(this.timeLeftMinute)
|
||||
+ ":" + this.getTwoDigitNumber(this.timeLeftSecond);
|
||||
else
|
||||
return "-" + this.getTwoDigitNumber(this.timeLeftHour)
|
||||
+ ":" + this.getTwoDigitNumber(this.timeLeftMinute)
|
||||
+ ":" + this.getTwoDigitNumber(this.timeLeftSecond);
|
||||
Timer.prototype.getTimeWithTimeFormat = function(timeValue, type) {
|
||||
if(type != Timer.TYPE_HOUR) {
|
||||
return this.getTwoDigitNumber(timeValue);
|
||||
}
|
||||
|
||||
if(this.timeLeft < 0) {
|
||||
return "-" + this.getTwoDigitNumber(timeValue);
|
||||
} else {
|
||||
return this.getTwoDigitNumber(timeValue);
|
||||
}
|
||||
}
|
||||
|
||||
Timer.prototype.sendTimeLeftToServer = function() {
|
||||
@@ -258,4 +322,8 @@ Timer.TIME_MINUTES_FOR_HOUR = 60;
|
||||
Timer.TIME_SECONDS_FOR_MINUTE = 60;
|
||||
|
||||
Timer.TIME_DEFAULT_SEC
|
||||
= Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE; // 60 min * 60 sec
|
||||
= Timer.TIME_MINUTES_FOR_HOUR * Timer.TIME_SECONDS_FOR_MINUTE; // 60 min * 60 sec
|
||||
|
||||
Timer.TYPE_HOUR = "hour";
|
||||
Timer.TYPE_MINUTE = "minute";
|
||||
Timer.TYPE_SECOND = "second";
|
||||
Reference in New Issue
Block a user