我正在创建 Flash 游戏。这是计时器,它计算玩家玩了多长时间。当“游戏结束”计数器停止。我需要把这个时间写到数据库中。我的计数器格式是 MM:SS,所以我不知道该怎么做。
这是我将数据从游戏发送到 php 的代码:
var urlReq:URLRequest = new URLRequest ("band.php");
// Set the method to POST
urlReq.method = URLRequestMethod.POST;
// Define the variables to post
var urlVars:URLVariables = new URLVariables();
urlVars.time = timer.currentCount;
urlVars.userName = "myUserName";
// Add the variables to the URLRequest
urlReq.data = urlVars;
// Add the URLRequest data to a new Loader
var loader:URLLoader = new URLLoader (urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoginComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
所以我将我的time
变量发送到 php。
这是获取时间的php代码:
<?php
$time = $_POST['time'];
$username = $_POST['userName'];
$con=mysqli_connect("localhost","my_db","pass","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO eurokos (time, userName)
VALUES ('$time', '$username')");
$query = "INSERT INTO eurokos VALUES" . "('$time', '$username')";
echo "success=true";
?>
所以用户名成功插入数据库,这意味着向php发送变量一切都很好,但时间总是“0”值,所以这意味着定时器有问题。
我的计时器看起来像:
function showTimePassed(startTime:int):String {
var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var leadingZeroM:String = "";
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing,
if (miliseconds < 10) { //if less than two digits, add a leading 0
leadingZeroMS = "0";
}
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
var minutes = Math.floor((time / (60 * 1000) ) );
if (minutes < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
//60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds ;
}
我不知道出了什么问题,为什么不插入数据库。你可以帮帮我吗?谢谢。
更新
<?php
$time = $_POST['time'];
$username = $_POST['userName'];
session_start();
$name = $_SESSION['vardas'];
$times = gmdate('H:m:s', $time);
$con=mysqli_connect("localhost","padekime_db","pass","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "INSERT INTO eurokos VALUES" . "('$times', '$username')";
echo "success=true";
if ($stmt = $mysqli->prepare("INSERT into eurokos (time, userName) VALUE (?,?) ")) {
$stmt->bind_param("i", $time);
$stmt->bind_param("s", $name);
$stmt->execute();
}
?>
更新 2
function startMemoryGame():void
{
addChild(CardContainer);
timer = new Timer(1000); //create a new timer that ticks every second.
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick
timer.addEventListener(TimerEvent.TIMER, resetTimer);
txtTime = new TextField();
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = "#E50041";
format.size = 22;
txtTime.border = true;
txtTime.borderColor = 0xFFFFFF;
//format.bold = true;
//txtTime.x = 250;
txtTime.width/2;
var stageCenter_x:Number = stage.stageWidth/2;
var stageCenter_y:Number = stage.stageHeight/2;
var textCenter_x:Number = txtTime.width/2;
var textCenter_y:Number = txtTime.height/2;
txtTime.x = stageCenter_x - textCenter_x;
txtTime.y = 55;
txtTime.autoSize = TextFieldAutoSize.CENTER;
txtTime.defaultTextFormat = format;
message_txt.autoSize = TextFieldAutoSize.CENTER;
message_txt.defaultTextFormat = format;
txtTime.text = setNull(0);
addChild(txtTime);
tmpTime = getTimer();
timer.start();
}
private function tick(e:Event):void {
txtTime.text = showTimePassed(tmpTime);
}
function setNull(startTime:int):String {
return "00:00";
}
function showTimePassed(startTime:int):String {
var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var leadingZeroM:String = "";
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing,
if (miliseconds < 10) { //if less than two digits, add a leading 0
leadingZeroMS = "0";
}
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
var minutes = Math.floor((time / (60 * 1000) ) );
if (minutes < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
//60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds ;
}
function getTimePassed(startTime:int):Number {
return (getTimer() - startTime) /1000;
}
这里将变量发送到php:
var urlReq:URLRequest = new URLRequest ("band.php");
// Set the method to POST
urlReq.method = URLRequestMethod.POST;
// Define the variables to post
var urlVars:URLVariables = new URLVariables();
urlVars.userName = 'myUsername';
urlVars.time = getTimePassed(startTime); // HERE I GET ERROR
// Add the variables to the URLRequest
urlReq.data = urlVars;
// Add the URLRequest data to a new Loader
var loader:URLLoader = new URLLoader (urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoginComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
}