Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to transfer the variables score from GameScreen.as to GameOverScreen.as.

The method from GameOverScreen.as is called getScore(score:Number), that doesn't update the finalScore from the constructor of GameOverScreen.as.

Is there the way to ensure that the finalScore has the same value as the scorePoints from the GameScreen.as? (P.S I tried finding the answer but there's no avail.)

Here's the code:

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);
    }


    }

  }
share|improve this question

1 Answer

The problem seems to be that your GameOverScreen only updates the ScoreText.text in the constructor. The constructor is only executed when the GameOverScreen is created.

Later, after the GameOverScreen has been created, you call the getScore() method and pass in the final score. But all the getScore() method does is update the finalScore variable... it does not actually update the text field with the actual score.

What you should probably do is remove this line from the constructor:

ScoreText.text = "" + finalScore;

And put it in your getScore() method. I would also consider renaming getScore() to setScore() -- because it is setting the score, not retrieving it...

public function setScore(score:Number)
{
    finalScore = score;
    trace(finalScore);
    ScoreText.text = "" + finalScore;
}
share|improve this answer
It can't update the finalScore and it gives NAN because I placed the ScoreText.text in the GameOverScreen's movieclip. Is there a way for it to change the score? I assume is putting setScore(finalScore) in the constructor but it got error. – Minelava 23 hours ago
Can you add the code for the main class? I'm not sure I follow what is happening from your comments. – Sunil D. 22 hours ago
Is it better if I can send you the code and the fla through email or dropbox? – Minelava 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.