Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

tutorial 2 - the properties of my class

This is a discussion on tutorial 2 - the properties of my class within the Object Oriented Programming - tutorials forums, part of the Tutorials category; Last time, we saw how to create an Actionscript 3.0 Class and we saw what is the use of ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Object Oriented Programming - tutorials

Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 27-09-07, 05:36
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,354
Blog Entries: 1
Rep Power: 6
Flep is on a distinguished road
tutorial 2 - the properties of my class

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 first learnt to define.

The class property allows us to store or save values in that precise 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 be called and used on demand during the carrying out (execution/running) of the code.

As an example, let us imagine to have 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.

This is when the properties are used.

So, we declare a property as we would do for a variable. Even more, that property inside the class can have attributes.
Each property of a class is declared before the constructor function. You are not obliged to do so but it is a good practice to declare them outside and before the class so that our class is easier to read and verified while working on it.

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 we have seen until now:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip
	{
		public function Second()
		{
			trace(" The class Second has been instantiated correctly ");
		}
	}
}
I
create the FLA associated to the class Second and save it as second.fla.
At this point, if we publish the SWF, we would have the trace that The class Second has been instantiated correctly.

I declare a property to the class Second the following way:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip;
	{
		private var ancient:Number=10;
		
		public function Second()
		{
			trace(" The class Second has been instantiated correctly");
		}
	}
}
Pay attention to this line: private var ancient:Number=10;
Yes, it is how it is. I declared a variable (property of the class) named ancient to 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 one will be explained further on during the next lessons.
var tells Flash to create a new variable
ancient is the name assigned to the property (you could call it anyway you wish)
:Number means that only numbers are accepted as value for that variable
ë=10í assigns the value equal to 10

To retrieve the value of the property ëancientí, we simply need to call it when needed.
For example:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip
	{
		private var ancient:Number=10;
		
		public function Second()
		{
			trace("The class Second has been instantiated correctly");
			trace( ancient );
		}
	}
}
We can also declare our property and we assign it a value later on, for example:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip
	{
		private var ancient:Number;
		
		public function Second()
		{
			trace("The class Second has been instantiated correctly");
			ancient=10;
			trace( ancient );
		}
	}
}
We can see that we first have declared the property ancient: private var ancient:Number; and then in the building function, we assigned it to the value 10.
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 much more further on with the lessons)

Let us do another example to make sure we have understood the concept that I am trying to explain:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip
	{
		private var ancient:Number=10;
		
		public function Second()
		{
			trace("The class Second has been instantiated correctlyî);
			ancient+=10;
			trace( ancient );
		}
	}
}
I declared the property assigning to it immediately the value equal to 10: private var ancient:Number=10; and in the building function I increased its value of 10. The output is as following:
The class Second has been instantiated correctly
20

If once we declared the property and its value and that we are sure that its value will not be modified anymore, we could use ëconstí (as constant) instead of ëvarí:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip
	{
		private var ancient:Number=10;
		private const modern:Number=30;
		
		public function Second()
		{
			trace("The class Second has been instantiated correctly");
			ancient+=10;
			trace( ancient );
			trace(modern);
		}
	}
}
I use ëconstí to tell Flash that the property will have a constant value and will not be changed next: private const modern:number=30;
If I would try to assign a new value to the constant ëmoderní, flash would return an error.

An important point to remember:

Flash CS3 assign 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 and assign to it an instance name ëclip_mcí.
In the building function of Second.as, I ask for a trace of clip_mc:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Second extends MovieClip
	{
		private var ancient:Number=10;
		private const modern:Number=30;
		
		public function Second()
		{
			trace("The class Second has been instantiated correctly");
			ancient+=10;
			trace( ancient );
			trace(modern);
			trace(clip_mc);
		}
	}
}
I obtain this output:
The class Second has been instantiated correctly
20
30
[object MovieClip]

Flash has recognised clip_mc placed on stage in second.fla.

In fact, we can even interact with ëclip_mcí:
Code:
package
{
    import flash.display.MovieClip;
    
    public class Second extends MovieClip
    {
        private var ancient:Number=10;
        private const modern:Number=30;
        
        public function Second()
        {
            trace(" la classe Second Ë stata istanziata correttamente ");
            ancient+=10;
            trace( ancient );
            trace(modern);
            trace(clip_mc);
            clip_mc.x=stage.stageWidth/2;
            clip_mc.y=stage.stageHeight/2;
        }
    }
}
I assigned values to the x and y properties of clip_mc:
clip_mc.x=stage.stageWidth/2;
clip_mc.y=stage.stageHeight/2;

To learn more about the interactivity in 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:
Attached Files
File Type: zip tutorial_2.zip (7.9 KB, 39 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 12:43.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 2 - gli Array Flep Actioscript 3.0 base - tutorials 24 29-07-08 18:40
Tutorial 1 the variables Flep Actionscript for beginners - tutorials 0 01-11-07 05:47
Tutorial 8 the constants Flep Object Oriented Programming - tutorials 0 30-10-07 05:47
Tutorial 1 - my first AS 3.0 class Flep Object Oriented Programming - tutorials 1 27-10-07 06:15
How to call another class from the Document Class with Flash CS3 Flep Tutorials 0 09-10-07 18:50


All times are GMT. The time now is 07:46.


Powered by vBulletin versione 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406