Fix: timer countdown

This commit is contained in:
2018-12-10 09:51:56 +09:00
parent e1ef0a1575
commit 2e51b5c13a
2 changed files with 101 additions and 34 deletions
-1
View File
@@ -65,7 +65,6 @@ Chart.drawText = function(posX, posY, text, fontStyle) {
Chart.prototype.printContents = function() { Chart.prototype.printContents = function() {
if(isDebugMode()) { if(isDebugMode()) {
console.log("printContents : " + this.records.length);
for(var i = 0; i < this.records.length; i++) { for(var i = 0; i < this.records.length; i++) {
this.records[i].printDate("12/10"); this.records[i].printDate("12/10");
this.records[i].printScore(360); this.records[i].printScore(360);
+101 -33
View File
@@ -11,9 +11,9 @@ function Timer() {
var timerCenterY = 183; var timerCenterY = 183;
this.timeHourText = this.makeTimerTextContent(timerCenterX - 240, timerCenterY, 160, "00"); 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.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"); this.timeSecondText = this.makeTimerTextContent(timerCenterX + 190, timerCenterY + 20, 100, "00");
// buttons // buttons
@@ -28,34 +28,36 @@ function Timer() {
// hour button // hour button
this.hourPlus10Button = this.makeTimePlusButton(hour10PosX, plusPosY, "▲", 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.hourPlus10Button.setInputEnabled(false);
this.hourMinus10Button = this.makeTimeMinusButton(hour10PosX, minusPosY, "▼", 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, "▲", 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, "▼", this.hourMinus1Button = this.makeTimeMinusButton(hour1PosX, minusPosY, "▼",
(function() { this.timeMinus(-1, "hour"); }).bind(this) (function() { this.timeMinus(-1, Timer.TYPE_HOUR); }).bind(this)
); );
// minute button // minute button
this.minutePlus10Button = this.makeTimePlusButton(minute10PosX, plusPosY, "▲", 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, "▼", 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, "▲", 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, "▼", 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() { Timer.prototype.initVariables = function() {
this.prevTime = null; this.prevTime = null;
this.timeLeft = 0; this.timeLeft = Timer.TIME_DEFAULT_SEC;
this.timeLeftHour = 0;
this.timeLeftMinute = 0;
this.timeLeftSecond = 0;
this.timerEvent = null; 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) { Timer.prototype.makeTimerTextContent = function(x, y, sizePx, content) {
// style setting // style setting
var timerTextStyle = { font: "bold 160px Arial", fill: "#fff", align: "right"}; 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) { Timer.prototype.timePlus = function(amount, type) {
console.log(amount + type); console.log(amount + type);
// if(type == "hour") // if(type == Timer.TYPE_HOUR)
this.timeLeft += amount * 60; this.timeLeft += amount * 60;
this.updateTimeText(); this.updateTimeText();
this.sendTimeLeftToServer(); this.sendTimeLeftToServer();
@@ -165,7 +225,7 @@ Timer.prototype.timePlus = function(amount, type) {
Timer.prototype.timeMinus = function(amount, type) { Timer.prototype.timeMinus = function(amount, type) {
console.log(amount + type); console.log(amount + type);
// if(type == "hour") { // if(type == Timer.TYPE_HOUR) {
// var totalMinusMinute = amount * Timer.TIME_MINUTES_FOR_HOUR; // var totalMinusMinute = amount * Timer.TIME_MINUTES_FOR_HOUR;
// if(totalMinusMinute - ) // if(totalMinusMinute - )
// } // }
@@ -202,13 +262,16 @@ Timer.prototype.timeDown = function(sec) {
// this.sendTimeLeftToServer(); // 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() { 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.updateTimeVariables();
// this.timerText.text = this.getTimeWithTimeFormat(); // this.timerText.text = this.getTimeWithTimeFormat();
} }
@@ -224,15 +287,16 @@ Timer.prototype.getTwoDigitNumber = function(value) {
return absValue; return absValue;
} }
Timer.prototype.getTimeWithTimeFormat = function() { Timer.prototype.getTimeWithTimeFormat = function(timeValue, type) {
if(this.timeLeft > 0) if(type != Timer.TYPE_HOUR) {
return this.getTwoDigitNumber(this.timeLeftHour) return this.getTwoDigitNumber(timeValue);
+ ":" + this.getTwoDigitNumber(this.timeLeftMinute) }
+ ":" + this.getTwoDigitNumber(this.timeLeftSecond);
else if(this.timeLeft < 0) {
return "-" + this.getTwoDigitNumber(this.timeLeftHour) return "-" + this.getTwoDigitNumber(timeValue);
+ ":" + this.getTwoDigitNumber(this.timeLeftMinute) } else {
+ ":" + this.getTwoDigitNumber(this.timeLeftSecond); return this.getTwoDigitNumber(timeValue);
}
} }
Timer.prototype.sendTimeLeftToServer = function() { Timer.prototype.sendTimeLeftToServer = function() {
@@ -258,4 +322,8 @@ Timer.TIME_MINUTES_FOR_HOUR = 60;
Timer.TIME_SECONDS_FOR_MINUTE = 60; Timer.TIME_SECONDS_FOR_MINUTE = 60;
Timer.TIME_DEFAULT_SEC 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";