2016-08-05 4 views
0
this.collision = function(p1, p2, p3, p4) 
{ 
    if(this.ballY - this.radius + this.dy <= p1.height && this.ballX + this.dx >= p1.leftPostx - p1.goalPostRadius && this.ballX <= p1.rightPostx + p1.goalPostRadius) 
    { 
     this.dy = -this.dy; 
     this.ballY = p1.height + this.radius; 
     count++; 
     score++; 
     <?php $_SESSION['score'] += 1; ?> 
    } 
    else if(this.ballY + this.radius + this.dy >= canvas.height - p2.height && this.ballX + this.dx >= p2.leftPostx - p2.goalPostRadius && this.ballX + this.dx <= p2.rightPostx + p2.goalPostRadius) 
    { 
     this.dy = -this.dy; 
     this.ballY = canvas.height - p2.height - this.radius; 
     count++; 
     score++; 
     <?php $_SESSION['score'] += 1; ?> 
    } 
    else if(this.ballX + this.radius + this.dx >= canvas.width - p3.height && this.ballY + this.dy >= p3.topPosty - p3.goalPostRadius && this.ballY + this.dy <= p3.bottomPosty + p3.goalPostRadius) 
    { 
     this.dx = -this.dx; 
     this.ballX = canvas.width - p3.height - this.radius; 
     count++; 
     score++; 
     <?php $_SESSION['score'] += 1; ?> 
    } 
    else if(this.ballX - this.radius + this.dx <= p4.height && this.ballY + this.dy >= p4.topPosty - p4.goalPostRadius && this.ballY + this.dy <= p4.bottomPosty + p4.goalPostRadius) 
    { 
     this.dx = -this.dx; 
     this.ballX = p4.height + this.radius; 
     count++; 
     score++; 
     <?php $_SESSION['score'] += 1; ?> 
    } 
    else if(this.ballY - this.radius + this.dy < 0 || this.ballY + this.radius + this.dy > canvas.height || this.ballX - this.radius + this.dx < 0 || this.ballX + this.radius + this.dx > canvas.width) 
    { 
     checkLives(p1, p2, p3, p4, ball1); 
    } 
} 

したがって、タイトルはそれをすべて示しています。このコードでは、セッション変数をインクリメントするときを判断するためにjavascript条件を使用しようとしています。これどうやってするの?みんな助けてくれてありがとう!JS内のPHPセッションを増やす

答えて

1

これはこのようにはできません。いつでもあなたのスクリプトの準備ができてロードし、PHPコードが実行されるので、あなたのjs関数呼び出しやjs条件に依存しません。 PHPはサーバー側のスクリプト言語で、JavaScriptはクライアント側のスクリプトです。

ajaxまたはjqueryの投稿を使用して試すことができます。

この方法のように試してみてください

..あなたがあなたのヘッダに追加することができ、その後のjQueryライブラリファイルを追加されていない場合は

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 

は今、あなたのコードは次のとおりです。

var myObj = new function(){ 
    this.collision = function(p1, p2, p3, p4) 
    { 
     if(this.ballY - this.radius + this.dy <= p1.height && this.ballX + this.dx >= p1.leftPostx - p1.goalPostRadius && this.ballX <= p1.rightPostx + p1.goalPostRadius) 
     { 
      this.dy = -this.dy; 
      this.ballY = p1.height + this.radius; 
      count++; 
      score++; 
     } 
     else if(this.ballY + this.radius + this.dy >= canvas.height - p2.height && this.ballX + this.dx >= p2.leftPostx - p2.goalPostRadius && this.ballX + this.dx <= p2.rightPostx + p2.goalPostRadius) 
     { 
      this.dy = -this.dy; 
      this.ballY = canvas.height - p2.height - this.radius; 
      count++; 
      score++; 
     } 
     else if(this.ballX + this.radius + this.dx >= canvas.width - p3.height && this.ballY + this.dy >= p3.topPosty - p3.goalPostRadius && this.ballY + this.dy <= p3.bottomPosty + p3.goalPostRadius) 
     { 
      this.dx = -this.dx; 
      this.ballX = canvas.width - p3.height - this.radius; 
      count++; 
      score++; 
     } 
     else if(this.ballX - this.radius + this.dx <= p4.height && this.ballY + this.dy >= p4.topPosty - p4.goalPostRadius && this.ballY + this.dy <= p4.bottomPosty + p4.goalPostRadius) 
     { 
      this.dx = -this.dx; 
      this.ballX = p4.height + this.radius; 
      count++; 
      score++; 

     } 
     else if(this.ballY - this.radius + this.dy < 0 || this.ballY + this.radius + this.dy > canvas.height || this.ballX - this.radius + this.dx < 0 || this.ballX + this.radius + this.dx > canvas.width) 
     { 
      checkLives(p1, p2, p3, p4, ball1); 
     } 

     myObj.updateCore(score); 
    } 
    this.updateCore = function(addIncrement){ 
      jQuery.ajax({ 
       dataType: "json", 
       method:"POST", 
       url:PATH_TO_FILE+'/serverfile.php', 
       data:{score:addIncrement}, 
       async:false, 
       beforeSend:function(){}, 
       success:function(data){}, 
       error:function(request, status, error) { 
       alert(request.responseText); 
       }, 
       complete: function(){} 
      }); 
     } 
    } 

でserverfile.php

<?php 

$_SESSION['score'] = $_POST['score']; 

?> 
+0

明日は、ありがとう! –

+0

だから、うまくいけば私はこれを正しく理解しています。これでJSスコア変数の値がserverfile.phpにポストされ、セッションが更新されます。 serverfile.phpにsession_start()も必要ではないでしょうか? –

+0

このファイルが含まれる前にあなたのコードの任意のファイルに入っていればあなたのフレームワークを知っていません。セッション開始が開始されてからセッションを開始する必要はありません。 –

関連する問題