Variables

Hi all,

Here’s a very simple question. How do you distinguish global and local variable? How about object property?

Let me explain my question abit more.

When I create some variable in the frame1 of main timeline, what is the scope of this variable? is this accessible from different timeline? also when I create variable within conditional, is is accessible from outside the brackets?

a variable created in the main timeline can be accessed by using _root.Variable or (i think) _level0.Variable

a variable created in a function cannot be accessed from outside of that function, but if you make something on a button, or a movieclip then it can be targeted as long as it is not contained within a function…

I THINK. I may be wrong… soemoen correct me…

oh, and who is that in your avatar?

*Originally posted by Jubba *
**I THINK. I may be wrong… soemoen correct me… **

heh :wink:

Heres the deal with variables. Unless otherwise specified, any variable assignment is done within the scope of the movieclip the code is being written on. If on the maintimeline, that variable is going to be set in _root. If in the timeline of a movieclip called Jones, the variable will be in the scope of that movieclip. So if Jones is on the main timeline, that variable would be

_root.Jones.variableName

The otherwise specified part comes from scoping with either dot syntax or from the with(Object){} command. Note that any non-existent variable in the Object specified of the with statement will be assigned to the movieclip the script is being placed. (Note scripts ON buttons are in the scope of the movieclip in which they exist; scripts ON movieclips are in the scope of THAT movieclip, and not the clip in which it exists which is the timeline you see in the Flash authoring environ)

this - what this does is lets you specify the scope to be within the currently referenced object of the current function block. Note that this is a with(Object){} is not the Object but the movieclip in which the code is placed. ex:


// on main timeline.  myMovieClip is the instance
// name of a movieclip on the screen
myMovieClip.myVariable = 0;
myMovieClip.onMouseDown = function(){
	myVariable = 2;
	this.myVariable = 1;
}

this sets two variables: a variable called myVariable in _root and a variable called myVariable in _root.myMovieClip. The myVariable in _root.myMovieClip starts out 0 but is changed to 1 in the onMouseDown event of that movieclip (where the _root variable is also set and is non-existant until then).

Functions though, can have local variables within themselves which ‘fizzle’ at the end of the function call. Here is where you would use the var keyword (otherwise it serves no real purpose)


// on main timeline.  myMovieClip is the instance
// name of a movieclip on the screen
test = function(){
	var tempVar = 0;
	myVariable = 1;

}
test();
trace(tempVar); // undefined
trace(myVariable); // 1

Note this is all for assignment. In terms of referencing, its a whole 'nother story…

thank u sen :wink:

Referencing works on levels - not Flash _levels, but levels of variable existence It first starts with your targeting and referencing with dot syntax (or with). If the variable exists within that scope, you get the value of the variable in that scope. If it doesnt exist, Flash doesnt stop there, it checks the next level up in the prototype chain. If it exists there, it checks the next level up and so on and so forth. Now, if you are in the scope you are attempting to reference (timeline) and you dont specify a targeting dot-syntax prefix to the variable call, the _global (top level) object will also be referenced after running through all the previous prototype chains if the variable was not found.

… however, heres where the _global mayhem comes into play. When you assign a global variable, you use
_global.varName = value;
if you ever need to reference that value, you can simply say
varName
and not have to use _global. BUT if you need to re-assign it, you’ll need to include the _global prefix or the variable will be assigned to the movieclip (timeline) in which you are writing your code.


// on _root
_global.test = 1;
trace(_global.test); // 1
trace(test); // 1
test = 2;
trace(_global.test); // 1
trace(test); // 2

here, because we are in _root, or that scope, and there was no targeting done to put the variable in the _global object, the variable is assigned to be a variable of _root, which then, when referenced over-rides the _global value since it is found before the reaching the _global version.

In terms of the protootype chain, a movieclip, for example, checks the following places:

  1. movieclip Instance
  2. movieclip.prototype
  3. object.prototype
  4. _global (if refrenced on that movieclip without a preceding prefix like myMovieclip.variableName)

ex:


// on _root
_global.myVar = "global";
Object.prototype.myVar = "Object proto";
MovieClip.prototype.myVar = "MovieClip proto";
myMovieInstance.myVar = "Var in actual instance";


// on myMovieInstance
onClipEvent(load){
	trace(myVar);
	trace(this.myVar +"
");
	delete myVar;
	trace(myVar);
	trace(this.myVar +"
")
	delete MovieClip.prototype.myVar;
	trace(myVar);
	trace(this.myVar +"
");
	delete Object.prototype.myVar;
	trace(myVar);
	trace(this.myVar);
}

this outputs:

Var in actual instance
Var in actual instance

MovieClip proto
MovieClip proto

Object proto
Object proto

global
undefined

The same ideals apply to normal non movieclip objects, however, the source of most variable referencing is typically done within movieclips as all variables referenced without a prefix references the variable in the movieclip in which the code is being typed.

Thank you Jubba and Senocular,

It’s much clearer now. I think this tracing scheme is just like a garbage collection as in Java’s exception handling. I have another question regarding variables. when you call, say onMouseDown();
you usually define method like,
something.onMouseDown = funcion(){
someProperty = 5;
otherProperty = “Mine”;
};
My question is what those method or function initially contain. Those property or variables do exist before defining by ourselves? are we just overriding them?

they do not exist until you define them.

Basically, whats happening is the onMouseDown function attempted for each movieclip that exists when the mouse is pressed. However, if that function is not defined, a null or undefined function is called which essentially means nothing. Its like trying to call a function which you didnt write.

myObj.yabazabalicious();

What happens when you try that? Nothing. (not unless you defined yabazabalicious which Im assuming you havent). Basically thats whats happening when you press the mouse. The Flash player shoots through all the movieclips in the movie and if a valid onMouseDown is recognized (and this can be in a prototype) its fired and properly called. Otherwise nothing happens because theres nothing to handle.

Same applies to onEnterFrame, onMouseUp and all that junk.
The first part of my ASBroadcasters article gets into the flash player and listening for events and all that. You might want to take a look at it.
http://www.umbc.edu/interactive/flash/tutorials/ASBroadcaster.php

Note in:

*Originally posted by KazchiX *
**
something.onMouseDown = funcion(){
someProperty = 5;
otherProperty = “Mine”;
};
**

someProperty and otherProperty are NOT being defined within the scope of something, but rather whatever timeline or movieclip that code is being written. If you wanted someProperty and otherProperty to be a part of something, you would have to preceed those property names with this:

something.onMouseDown = funcion(){
[color=blue]this[/color].someProperty = 5;
[color=blue]this[/color].otherProperty = “Mine”;
};

Did you write that article? That’s like the whole chapter of listener explanation there. I’m impressed. I started flash about two weeks ago and pretty new to this world but it becomes more into object-oriented language in a sence. I like the way model-view-controler paradigm does. According to your article, flash MX now allow us to modify object. How about pre-defined object? Can we override them? Modifying actionlistener would be fun to do. Well, I’m just curious. Thanks for the article. That’s one of the best reference out there.

I’m still reading your article but there’s little confusion over ASBroadcaster. In the article, ASBroadcaster holds all the actionlistner. Is that correct? and manipulating ASBroadcaster will re-define the way actionlistener works. Then how ASBroadcaster obtain the source of action? am I making this clear? Well, this might be beyond my reach.

Most pre-defined objects can be in some shape or form be over-written or at least over-ridden. A lot depends on what object you’re dealing with and what it is you want to alter. A lot of that can be determined just by testing and finding out what happens when you try. Technically I could sit here and list it all out for you but that would take a while :wink:

As for ASBroadcaster - all it represents is a methodogy in programming behavior. What it accomplishes is nothing which cant be done manually without the object - as you can see in the Flash 5 interpretation of the object where there, it didnt yet exist. As with what the Santa Clause example tries to illustrate, the “listeners” are nothing but an array of objects which get cycled through and have run a method call attempt. If that method exists, it will be called, if not, nothing will happen… just like with the onMouseDown. Similarly to how the Flash Player checks the existance of the onMouseDown functions within the movieclips in order to execute them or not, The ASBroadcaster object does the same thing, only it cycles through not all the movieclips in the movie, but all the movieclips (or other objects for that matter) in its listeners list. that object then has its broadcasted method called (if existant) which is run as an ‘event’. Santa Clause only attempts to give presents to the children on his list, not all the children in the world.

So what you have is an object (the ASBroadcaster intialized object) which is told what function (broadcastmessage) should it try to call for all of the objects (listeners) in its list. It cycles through that list and calls, or tries to, each function by that name in that object in the list. If its there, its run, if not, nothing happens. When that call is run, is up to you.

That makes sence alot. Flash is more sophisticated than I thought it was. Thank you so much.

Welcome

*Originally posted by Jubba *
**thank u sen :wink: **

You too Jubba!!