Minelava
Minelava

Reputation: 221

Passing variable from class to class and store the variable Actionscript 3.0

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


    }

  }

Upvotes: 0

Views: 98

Answers (1)

Sunil D.
Sunil D.

Reputation: 18193

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

Upvotes: 1

Related Questions