Question about AS1 OOP: Object Basics by Senocular

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

newObject(5);

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
this.createEmptyMovieClip(“temp”,1);

// 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:

:p:

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?

:thumb:

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
myArray.switchValues();
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.

:sen: well it’s already past advanced for me, but I figure it can’t hurt. I am waiting on my Macromedia Flash MX Designer’s AS Reference guide (slow mail…) so why not try to get as much out of your OOP Basics as I can? I’m not driving you crazy with my questions…(which may or may not even be too relevant to the lessons) am I?

not really, I just think it would be easier for you if you had some simpler background to work off of. If you’re just starting off, you may just be confusing yourself with all this junk here.

Yes you are probably right. I have been working with Flash for about 2 years now, BUT I really only get to use it once a year for anything worth while so I don’t get much practice. I figure I will follow through with it anyway and see what I can get out of it.

array is a drop of golden sun right?

see…I’m already learning something :hugegrin: