Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 4 of 4

Thread: tutorial 2 - the properties of my class

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    tutorial 2 - the properties of my class

    flash templates
    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:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Second extends MovieClip
    	{
    		public function Second()
    		{
    			trace("The class Second has been instantiated successfully");
    		}
    	}
    }
    I
    create the FLA file 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 successfully'.

    I declare a property to the class Second the following way:
    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");
    		}
    	}
    }
    Pay attention to this line: private var old:Number=10;
    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:
    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 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;
    		
    		public function Second()
    		{
    			trace("The class Second has been instantiated successfully");
    			old=10;
    			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.
    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:
    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 );
    		}
    	}
    }
    I declared the property assigning to it immediately the value 10;
    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:
    Code:
    The class Second has been instantiated correctly
    20
    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:
    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 );
    		}
    	}
    }
    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:
    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':
    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 );
    		}
    	}
    }
    I obtain the following output:
    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':
    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;
    		}
    	}
    }
    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 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

  2. #2
    Junior Member Settled In plashmaddy is on a distinguished road
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Re: tutorial 2 - the properties of my class

    Thank you! I am learning... :)

  3. #3
    Junior Member Settled In ksyz is on a distinguished road
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Re: tutorial 2 - the properties of my class

    thanks..

  4. #4
    Junior Member Settled In sivagrid is on a distinguished road
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Re: tutorial 2 - the properties of my class

    thanx for sharing oops concept

+ Reply to Thread

Similar Threads

  1. Storing current x properties of movie clip
    By Elva in forum Actionscript 3.0 newbies
    Replies: 3
    Last Post: 25-02-10, 08:47
  2. Tutorial 1 - my first AS 3.0 class
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 6
    Last Post: 27-01-10, 05:40
  3. tutorial 4 - properties attribute
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 1
    Last Post: 29-04-09, 09:41
  4. Tutorial 1 - le variabili
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 19
    Last Post: 23-04-09, 13:08
  5. Replies: 0
    Last Post: 09-10-07, 18:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts