Question about AS1 OOP: Object Basics by Senocular

Okay I am only at the very beginning and I want to make certain that I go through this understanding everything that can so maybe I won’t have to ask as many questions in the forums and can start answering more. In this part:

[SIZE=1][COLOR=Navy]The code for making a standard generic object in Flash is as follows:

myObject = new Object();

The variable myObject then becomes a object variable which can then have more variables added to it using dot syntax like so.[/COLOR][/SIZE]

Is myObject the actual name of the variable and the = new Object simply stating that there is a new object called myObject?

correct. “myObject” is the variable name. The variable “myObject” is of the type Object. It is defined as such because it was set to equal (=) “new Object()”

So “new Object()” is the command that creates the actual object in Flash. It is basically always the same for a generic object type (there is a shorthand version which I think I mention in there as well). “myObject” is your own personal variable name which can be anything. Its the container for keeping the object created so that you may use that object any way you see fit through that “myObject” variable.

:hugegrin: Thanks Senocular! Back to your tutorial…

np :wink: Feel free to ask any more questions as they arise.

Okay well I’m feeling free…

:jail: (okay maybe not quite so free)

Anyway, how come if I do not put a space between new and object in the AS below, it traces one instead of six?

[COLOR=Navy][SIZE=1]myObject = new Object(5);
trace(myObject + 1); // traces 6 [/SIZE] [/COLOR]

new and Object are two separate keywords in Flash. If you omit the space, you turn it into one keyword newObject <- and that isnt understood by Flash. In using

new Object(5);

you create a generic object with a base value of 5 - or really, a number object whose value is 5. Flash sees both the new keyword and the Object keyword seperately, understands them as such and creates the object as instructed. Then you get 5 + 1 is 6. If you use


Flash sees that as one word, “newObject” and doesn’t know what newObject is in Flash terms. Because of that, the return value is actually undefined or “nothing.” When you add 1 to nothing (Flash MX) you get 1.

I see. Once again thank you for the explanation. :pleased:

Okay in this example:

[COLOR=Navy][SIZE=1]// create a temporary movieclip

// assign a reference variable
tempRef = temp;
trace(temp); // traces _level0.temp
trace(tempRef); // traces _level0.temp [/SIZE] [/COLOR]

I don’t understand how the trace is returning level zero when the instance of the MovieClip is created in level one. (or at least I think that is what is happening)

i believe that _level0 is _root, and so it’s just saying you can acces temp from _level0 (_root).temp

but, i dunno oop, so… :slight_smile:


that is correct. Flash, when tracing movieclips, returns a path to that movieclip as its set within the Flash movie. Because you can load other Movies into Flash with their own _root timelines, Flash defaults each trace path to their respective levels. _level0 represents _root of the main timeline. _level1 would be _root of a swf loaded into level 1.

okay well what about this one- would this be right?

[COLOR=Navy][SIZE=1]num1 = 5;
num2 = 5;
trace(num1 == num2); // traces true

num1 = {5};
num2 = {5};
trace(num1 == num2); // traces false[/SIZE][/COLOR]

Or would they both trace true because the object is a number? And if they would not both trace true could someone please explain why?


Senocular you must have been submitting a reply at the same time I was. So as far as my question concerning the level thing, it is just saying that the temp movieclip can be accessed through the main timeline even though it was loaded into level1 (of the main timeline) right?

yes, any movieclip/movie can access movieclips in other levels of that same movie.

The current traces are correct (though for the second part you probably mean new Object(5)). Because the first compares basic variable types, or non-object variables, their values are compared and true is traced because 5 equals 5. When you’re dealing with objects, however, their comparative value is the object reference and not the value of that object itself. This means that when two objects are compared, their references are compared, or the “value” of the actual variable itself… and if that variable is an object, then its a reference to the object in Flash’s memory since its represented as a complex data type.

What is the difference between the two expressions below (where they both trace 50)?

[COLOR=Navy][SIZE=1]// basic number
num = 5;
trace(num.valueOf()); // traces 5 [/COLOR]

[SIZE=1][COLOR=DarkRed]// math expressions use valueOf
trace(num * 10); // traces 50;
trace(num.valueOf() * 10); // traces 50; [/SIZE] [/COLOR] [/SIZE]

Why would you use one over the other or are they different?

when you make your own custom objects, those which dont have a base value like Number objects do, you can create your own valueOf methods to give you whatever value you want. valueOf just gives you a function to manually get an object’s “value.” When you’re dealing with number variables, its usually pretty pointless because the variables are their values, but it can be helpful when making custom objects to give that object a value of its own based on whatever you want it to have (supplied through the valueOf function). I think its covered more as you read throughout the tutorial.

[COLOR=Navy][SIZE=1]myArray = [1,2];
myArray.switchValues = function(){
var temp = this[0];
this[0] = this[1];
this[1] = temp;
trace(myArray); // traces 1,2
trace(myArray); // traces 2,1

As you can see, this function uses this to directly access the current object’s (myArray’s) array elements as this refers to the array itself. Then, using a local temporary variable temp, it is able to internally switch the two values of those array elements. The trace after the function call confirms its success on the myArray variable.[/SIZE][/COLOR]

In this example, why is the variable temp needed to switch values :h:

do you know a way to do it without using a temp variable? Its just a necessity of the action. You need the temporary variable because if you assign 1 to 0 the original value of 0 is lost thereby not giving you a method for putting that original 0 value into 1. the temp variable saves the value so when its needed it can be put respectively into the other variable.

No I don’t know how to do it without a temp varible…actually I don’t know how to do it all at :crazy:
That’s why I am asking all the questions cause I am trying to understand everything :drool:

are you sure you want to be reading that tutorial? :wink: I think it will begin to get a little advanced for you fairly quickly.