将变量从类传递给类并存储变量Actionscript 3.0

时间:2013-06-02 17:44:06

标签: oop function variables actionscript constructor

我想将变量得分从 GameScreen.as 转移到 GameOverScreen.as

来自 GameOverScreen.as 的方法称为 getScore(得分:数字),不会更新 finalScore < / strong>来自 GameOverScreen.as 的构造函数。

是否有办法确保 finalScore GameScreen.as 中的 scorePoints 具有相同的价值(P.S我尝试找到答案,但没有用。)

以下是代码:

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 个答案:

答案 0 :(得分: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;
}