0

フラッシュゲームを作っています。これはタイマーで、プレイヤーのプレイ時間をカウントします。「ゲームオーバー」になるとカウンターが止まります。今回はデータベースに書き込む必要があります。私のカウンター形式は 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);
}
4

1 に答える 1

0

MySql は文字列 "MM:SS" として時間を受け取り、時間フィールドのデータ型は INT であるため、mysql の時間の値は常に 0 です。

あなたには2つの解決策があります:挿入クエリの前にPHP側でMM:SSを秒数に変換するか、フラッシュ側から合計秒数を送信します(私のPOVからより良い)。

更新:タイマーインスタンスから直接経過時間を取得

[...]

urlVars.time = timer.currentCount; // time elapsed in seconds

PHP 側では、準備済みステートメントを使用します。

更新: DBの時間/ユーザー名を節約するための完全なスクリプト[...]

$time = $_POST['time'];
$username = $_POST['userName'];

$mysqli = new mysqli("localhost","padekime_db","pass","my_db");

if ($stmt = $mysqli->prepare("INSERT into eurokos (time, userName) VALUES (?,?) ")) {

   $stmt->bind_param('is',$time,$username);

   $stmt->execute();

   if ($stmt->error != '') {
       echo ' error:'.$stmt->error;
   } else {
       echo 'success';
   }
   $stmt->close();
} else {
   echo 'error:'.$mysqli->error;
}

traceハンドラーで値を取得すると、loader.dataonLoginCompletesuccess」または「error:エラーの説明」が表示されるはずです。

http://www.php.net/manual/en/mysqli-stmt.prepare.php

于 2013-05-09T14:03:13.430 に答える