Thank you! I am learning... :)
Last time, we saw how to create an Actionscript 3.0 Class and we saw what is the use of the constructor of a class.
Now, we can see in more details the property of the class we previously learnt to define.
The class property allows us to store or save values in that class. It would be nothing else than the ActionScript variables we used to define in a flash movie as seen always. The only difference is that inside a class, those variables are called properties.
Those saved values can then be called and used on demand during the carrying out (execution/running) of the code.
As an example, let us imagine having a class which basically loads an external image. Once the image is completely loaded, we can then retrieve its dimensions. Those dimensions should then be stored to be reused when needed.
That is when the properties are used.
So, we declare a property as we would do for a variable. In addition, that property inside the class can have attributes. It's always a good practice to declare properties before the constructor function. That way our class is easier to read, verify and debug(fix). However, you are not obliged to do so.
Let us carry on
We saw how to define a building function of a class: public function ClassName().
I now create another AS file and save it as 'Second.as', defined as follows:
create the FLA file associated to the class Second and save it as 'second.fla'.Code:package { import flash.display.MovieClip; public class Second extends MovieClip { public function Second() { trace("The class Second has been instantiated successfully"); } } } I
At this point, if we publish the SWF, we would have the trace that 'The class Second has been instantiated successfully'.
I declare a property to the class Second the following way:
Pay attention to this line: private var old:Number=10;Code:package { import flash.display.MovieClip; public class Second extends MovieClip; { private var old:Number=10; public function Second() { trace("The class Second has been instantiated successfully"); } } }
Yes, it is how it is. I declared a variable (property of the class) named 'old' for the class Second.
private is an attribute meaning that the value of that property is not accessible from outside the class itself. There are 3 attributes available. As for now, we will only use the private one and the two remaining ones will be explained later on during the next lessons.
var tells Flash to create a new variable
old is the name assigned to the property (you could choose whatever name)
: means (of type)
Number means that only numbers are accepted as value for that variable
=10 assigns the value 10 to our 'old'
To retrieve the value of the property 'old', we simply need to call it when needed.
For example:
We can also declare our property and assign it a value later on, for example:Code:package { import flash.display.MovieClip; public class Second extends MovieClip { private var old:Number=10; public function Second() { trace("The class Second has been instantiated successfully"); trace( old ); } } }
We can see that we first have declared the property 'old' as private var old:Number; and then in the constructor, we assigned it to the value 10.Code:package { import flash.display.MovieClip; public class Second extends MovieClip { private var old:Number; public function Second() { trace("The class Second has been instantiated successfully"); old=10; trace( old ); } } }
This way, it is easier to understand that once the property is declared, we can interact with it as needed.
Another way is to call or assign a value to the property inside the methods (explained in the next lesson) or via getters & setters (seen in upcoming lessons)
Let us do another example to make sure we have understood the concept that I am trying to explain:
I declared the property assigning to it immediately the value 10;Code:package { import flash.display.MovieClip; public class Second extends MovieClip { private var old:Number=10; public function Second() { trace("The class Second has been instantiated successfully"); old+=10; trace( old ); } } }
private var old:Number=10; and in then the constructor, I increased its value by another 10. If you run the 'second.fla' movie, the output would be as follows:
If we want to declare a property and assign it a fixed value (meaning that we are sure that the value need not to be modified), we could use the 'const' (which stands for constant) instead of 'var':Code:The class Second has been instantiated correctly 20
I use 'const' to tell Flash that the property will have a constant value throughout my code and won't be allowed to be altered later:Code:package { import flash.display.MovieClip; public class Second extends MovieClip { private var old:Number=10; private const recent:Number=30; public function Second() { trace("The class Second has been instantiated successfully"); old+=10; trace( old ); trace( recent ); } } }
private const recent:number=30;
If I try to assign a new value to the constant 'recent', Flash would return an error.
An important point to remember:
Flash CS3 assigns, as property of the Document Class, any object contained on stage of the FLA associated to the Document Class.
I will explain myself better with an example:
On stage of the second.fla, I create a MovieClip object and give it an instance name of 'clip_mc'.
In the constructor of Second.as, I ask for a trace of the instance 'clip_mc':
I obtain the following output:Code:package { import flash.display.MovieClip; public class Second extends MovieClip { private var old:Number=10; private const recent:Number=30; public function Second() { trace("The class Second has been instantiated successfully"); old+=10; trace( old ); trace( recent ); trace( clip_mc ); } } }
Code:The class Second has been instantiated successfully 20 30 [object MovieClip]
Flash has recognised clip_mc placed on stage in second.fla.
In fact, we can even interact with 'clip_mc':
I assigned values to the x and y properties of 'clip_mc':Code:package { import flash.display.MovieClip; public class Second extends MovieClip { private var old:Number=10; private const recent:Number=30; public function Second() { trace("The class Second has been instantiated successfully"); old+=10; trace( old ); trace( recent ); trace( clip_mc ); clip_mc.x=stage.stageWidth/2; clip_mc.y=stage.stageHeight/2; } } }
clip_mc.x=stage.stageWidth/2;
clip_mc.y=stage.stageHeight/2;
To learn more about the interactivity between the Timeline and the Document Class, I recommend you to read this article: call from the timeline to the Document Class and vice versa.
Source files:
Last edited by Abdul; 16-01-09 at 13:33.
Thank you! I am learning... :)
thanks..
thanx for sharing oops concept
Bookmarks