2012-07-10 15 views
0
var scoreTotal:String = "Global"; //in the engine but not in the package 

if (currentKey is Left && leftKey) //in each level to score points to score on stage and scoreTotal 
{ 
    score += scoreBonus; 
    scoreTotal += scoreBonus; 
    currentKey.active = false; 
} 

public var score7:int = scoreTotal;// This is in the last level to print the score 

我得到錯誤1120:訪問未定義的屬性scoreTotal。全球var從水平收集分數,然後顯示在底部

任何人都可以幫忙嗎?

回答

1

使用全局變量不是一個好主意,在AS3中沒有這樣的事情。相反,創建一個Score類,其中包含跟蹤分數相關的任何內容。在你的主應用程序中保存一個這個類的實例。然後使用事件和監聽器通知,導致比分被更新的遊戲事件的應用:

public class Score { 
    private var total:int; 
    private var levels:Array; 

    public function addPoints (level:int, points:int) : void { 
     total += points; 
     levels[level] += points; 
    } 

    public function get scoreTotal() : int { 
     return total; 
    } 

    public function getLevelScore(level:int) : int { 
     return levels[level]; 
    } 

    public function Score(numLevels:int) : void { 
     total = 0; 
     levels = []; 
     var i:int = -1; 
     while(++i < numLevels) levels[i] = 0; 
    } 
} 


public class Main { 
    private var score:Score = new Score(7); 

    private var gameEngine:GameEngine; 

    .... 


    private function initGameScore() : void { 
     gameEngine.addEventListener (GameEvent.SCORE, onGameScore); 
     gameEngine.addEventListener (GameEvent.BONUS, onGameBonus); 
    } 

    private function onGameScore(ev:GameEvent) : void { 
     addPoints(ev.points); 
    } 

    .... 
} 

當然,GameEvent必須從flash.events.Event派生,幷包含一個字段points:int。只要發生任何值得評分的事情,您就可以派發GameEngine中的那些人。

然後,更高級的版本是保留一個事件和點的散列表,並且使得實際得分(即事件到點的映射)獨立於GameEngine而發生。