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。

任何人都可以帮忙吗?

4

1 回答 1

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并包含一个 field points:int。每当发生任何值得评分的事情时,您都会从 GameEngine 发送这些。

那么,更高级的版本是保存事件和点的哈希表,并使实际的得分(即事件到点的映射)独立于 GameEngine 进行。

于 2012-07-10T21:46:54.167 回答