2013-06-02 72 views
0

我要傳送的變量從GameScreen.asGameOverScreen.as得分。傳遞變量從類類和存儲變量的ActionScript 3.0

GameOverScreen.as方法被稱爲getScore(評分:號碼),不從GameOverScreen.as構造更新finalScore

是否有保證finalScore具有相同的值scorePointsGameScreen.as的方式嗎? (PS。我試着找到了答案,但有沒有用)

下面的代碼:

GameScreen.as

package 
{ 
import flash.display.MovieClip; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.geom.Point; 
import flash.net.SharedObject; 
import flash.utils.Timer; 
import flash.events.TimerEvent; 
import flash.text.*; 

/** 
* ... 
* @author xxxx 
*/ 
public class GameScreen extends MovieClip 
{ 
    private var mainClass:main; 
    private var enemy:Enemy; 
    private var timer:Timer; 
    public var scorePoints:Number; 

    public var gameOverScreen:GameOverScreen; 

    private var debugValue:Number; 

    public function GameScreen(passedClass:main) 
    { 
     mainClass = passedClass; 

     gameOverScreen = new GameOverScreen(); 

     enemy = new Enemy(); 
     addChild(enemy);       

     debugValue = 1; 

     scorePoints = 0; 

     addEventListener(Event.ADDED_TO_STAGE, onAdd); 

     ClickToWinButton.addEventListener(MouseEvent.CLICK, clickToWin); 


    } 

    private function clickToWin(e:MouseEvent):void 
    { 
     scorePoints += 50; 

    } 


    private function onAdd(e:Event):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, onAdd); 
     init(); 
    } 

    private function init():void 
    { 
     addEventListener(Event.ENTER_FRAME, gameScreenProgress); 
    } 

    public function gameScreenProgress(e:Event):void 
    { 
     ScoreText.text = "Score: " + scorePoints; 

     if (enemy.hitTestPoint(mouseX, mouseY, true)) 
     { 

      mainClass.showGameOver(); 

      gameOverScreen.getScore(scorePoints); 

      enemy.removeEnemy(); 

     } 
    } 


    } 

    } 

GameOverScreen.as

package 
{ 
import flash.events.Event; 
import flash.display.MovieClip; 
import flash.net.SharedObject; 
import flash.text.*; 
/** 
* ... 
* @author xxxx 
*/ 
public class GameOverScreen extends MovieClip 
{ 
    private var mainClass:main; 

    private var gameScreen:GameScreen; 

    public var finalScore:Number; 

    public function GameOverScreen() 
    { 


     ScoreText.text = "" + finalScore; 

    } 

    public function getScore(score:Number) 
    { 
     finalScore = score; 
     trace(finalScore); 
    } 


    } 

    } 

回答

1

這個問題似乎是你的GameOverScreen只更新構造函數中的ScoreText.text。當創建GameOverScreen時,構造函數爲,僅執行

之後,在創建GameOverScreen之後,您可以調用getScore()方法並傳入最終得分。但所有的getScore()方法所做的更新finalScore變量... 它實際上並沒有更新文本字段的實際得分

什麼你應該做的是從構造函數中刪除此行:

ScoreText.text = "" + finalScore; 

而且把它放在你的getScore()方法。我還要考慮重命名getScore()setScore() - 因爲它是設置分數,而不是獲取它...

public function setScore(score:Number) 
{ 
    finalScore = score; 
    trace(finalScore); 
    ScoreText.text = "" + finalScore; 
} 
+0

它無法更新finalScore它給NAN因爲我把ScoreText.text在GameOverScreen的影片剪輯。有沒有辦法讓它改變分數?我假設將setScore(finalScore)放在構造函數中,但它有錯誤。 – Minelava

+0

你可以添加主類的代碼嗎?我不知道我是否按照你的評論發生了什麼。 –

+0

如果我可以通過電子郵件或收件箱向您發送代碼和fla,它會更好嗎? – Minelava