John
John

Reputation: 1

Actionscript 3 - How to access integer variable in main timeline from inside MovieClip

First, sorry if my english is bad and I'm new to Actionscript 3. I have a movieclip, I give it an instance name "symbol1" which include a code inside it, like this :

var a: int = 2;
var b: int = 3;
var total: int = a + b;

How can I access the "total" integer variable inside that movieclip in the main timeline? I try to trace it but the result is 0. In the main timeline I wrote this code :

trace(symbol1.total);

I appreciate the help, thanks

Upvotes: 0

Views: 173

Answers (1)

null
null

Reputation: 5255

The "problem" is the order of execution of frames. The parent is executed first.

The value is 0 and not undefined because the property does indeed exist since the creation of the object which happens in a previous separate step.

You should be able to verify that by stepping through your code with the debugger.

I've made 6 games. each game is in the MovieClip including the game functions, times, scores, variables, etc. I want the total score of each game summed outside each MoviClips, which is in the main timeline. is there a solution to that problem?

This makes your timing problem even more apparent. You arrive at a frame, which contains a game and you immediately want to access the total score of it. It should be obvious that the game first has to be played in order to obtain a score from it.

Asking a newborn baby what his favourite moment in the first 60 years of his live was doesn't make much sense.

You have to notify the main timeline that the game was finished. The best way to do this is to dispatch an Event from the game. Frames are totally unnecessary.

Your code could look something like this:

var game:Game = new Tetris();
addChild(game);
game.addEventListener(GameEvent.FINISH, onFinish);

function onFinish(e:GameEvent):void
{
    trace("finished game with score " + game.score);
}

If you want to update the exterior score sum while the game is running (to immediately reflect any change in the score, not just when the game is finished), your best bet is to create a ScoreModel for the score which you pass to the game, the Game. The Game modifies the ScoreModel, which in turn causes it to dispatch Events:

var score:ScoreModel = new ScoreModel();
var game:Game = new Tetris(score);
addChild(game);

score.addEventListener(Event.CHANGE, onScoreChange);

function onScoreChange(e:Event):void
{
    trace("score changed to " + score);
}

Both examples are "what your code could look like"-type of examples. There are many questions about how to dispatch events that explain how to do this in more detail.


Something that came up in the comments from @Jezzamon:

I'm pretty sure you're accessing the variable total correctly, otherwise I would expect it to cause an error (you can test this by trying to run trace(symbol1.variableNameThatDoesntExist)).

No, there cannot be an error for accessing a variable as seen in the question. The reason for that is that the property is accessed on a MovieClip object, which is a dynamic class. As such, the compiler doesn't know if accessing a particular property is valid, because that can change at runtime. And because nothing else relies on the value to be valid, there isn't a runtime error either.

Here's some example code to illustrate this:

trace("toString: " + {}.toString); //toString: function Function() {}
trace("bananas: " + {}.bananas); //bananas: undefined

This is one of the reasons why using MovieClip can be a bad idea. In addition to that, the Flash IDE modifies the code behind the scenes. That can lead to unexpected execution of code. A purely code based workflow sure is advantageous in this regard and recommended.

Upvotes: 1

Related Questions