2012-08-24 19 views
1

我在我的Flash遊戲中有一個名爲score的變量,我想將我的Flash遊戲的得分發布到名爲test.php的php文件中;在AS3代碼的最後一部分,當遊戲結束是:發送AS3得分變量到PHP文件

function gameFinished(){ 
gameOver.play(); 
stage.removeEventListener(MouseEvent.CLICK, kick); 
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor); 
Mouse.show(); 
again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain); 
} 

這裏就是我想我的分數VAR發佈到PHP文件,所以我可以在排行榜使用它;我讀過很多教程,但是很難過。你能指導我一點點嗎?

回答

4

AS3:

function gameFinished(){ 
    ... 
    again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain); 

    var urlLoader:URLLoader = new URLLoader(); 
    var req:URLrequest = new URLRequest("test.php"); 
    var requestVars:URLVariables = new URLVariables(); 
    requestVars.score = 150; //your scroe here 
    req.data = requestVars; 
    req.method = URLRequestMethod.POST; 
    urlLoader.load(req); 
    urlLoader.addEventListener(Event.COMPLETE, scoreSent); 
} 

function scoreSent(e:Event){ 
    trace("score sent to php"); 
} 

PHP:

if(isset($_POST["score"])){ 
    //submit the score; 
} 
+0

非常感謝mgraph,你的代碼的工作,我真的很感激它,唯一的問題是,後由時打開一個新窗口,我的意思是test.php文件在另一個瀏覽器窗口中打開,關於如何避免這個問題的任何想法? –

+0

它不應該打開一個新窗口,是否有某處navigateToURL? – Zhafur

+0

@ i'llbdevsomeday確保你沒有'naviagateToURL(「test.php」);'在你的as3腳本中 – mgraph