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 1 of 1

Thread: Tutorial 8 the constants

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

    Tutorial 8 the constants

    flash templates
    After having seen the properties (variables inside an Actionscript 3.0 class), we can now introduce the constants.
    The constants are a new characteristic implemented in the version 3.0. In fact, with the 2.0 they were not present.

    For convention, they are declared using a name in capital letters so to understand at first sight that we are dealing with constants and not variables (properties).

    We can define the value of a constant but we cannot modify it.

    If we use more words as the constant name, it is of good rule to add an underscore in between the words.

    Examples:
    - Event.COMPLETE, COMPLETE is a constant of the Event class.
    - MouseEvent.CLICK, CLICK is a constant of the MouseEvent class.


    Let us look at it with even more examples'

    I create a FLA and save it as 'ottava.fla'.
    I create the Document Class associated to the FLA and save it as 'Ottava.as', implemented the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Ottava extends MovieClip
    	{
    		public function Ottava()
    		{
    			
    		}
    	}
    }
    I will now declare the constant:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Ottava extends MovieClip
    	{
    		private const NOME:String='Filippo';
    		
    		public function Ottava()
    		{
    			
    		}
    	}
    }
    private const NOME:String='Filippo';

    - private :we do know by now the meaning of it
    - const :declares to Flash that it is a constant and not a variable
    - NOME :the constant's name in capital letter
    - :String :is the type of value accepted by the constant (exactly as a variable)
    - ='Filippo'; :the constant's value

    We can access to the content of the constant in the same way as for the content of a variable:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Ottava extends MovieClip
    	{
    		private const NOME:String='Filippo';
    		
    		public function Ottava()
    		{
    			trace(NOME);
    		}
    	}
    }
    with trace(NOME);

    I obtain the following output:
    Filippo

    It is not possible to assign a value to a constant after its declaration.
    As an example, I declare another constant:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Ottava extends MovieClip
    	{
    		private const NOME:String='Filippo';
    		private const ETA:int;
    		
    		public function Ottava()
    		{
    			trace(NOME);
    		}
    	}
    }
    if I publish the SWF, Flash would return the following error:
    Warning: 1110: The constant was not initialized.
    It simply tells us that we did not assign a value to the constant.

    In fact, if we try to assign a value from the building function:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Ottava extends MovieClip
    	{
    		private const NOME:String='Filippo';
    		private const ETA:int;
    		
    		public function Ottava()
    		{
    			trace(NOME);
    			ETA=35;
    		}
    	}
    }
    Flash would return the following error:
    1049: Illegal assignment to a variable specified as constant.
    It tells us that we are trying to assign a value to a constant in a forbidden way, meaning after the constant's declaration.

    About the constant's attributes'
    They are the same applied to the variables:
    private, internal, protected, public and static
    I recommend to look at tutorial 4 if you have any doubt about the variable's attributes.

    Source files:
    Attached Files

+ Reply to Thread

Similar Threads

  1. Tutorial 4 - le funzioni
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 6
    Last Post: 13-09-09, 15:45
  2. Tutorial 3 - the loops
    By Flep in forum Actionscript for beginners - tutorials
    Replies: 1
    Last Post: 17-06-09, 21:49
  3. Tutorial 3 - i cicli
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 12
    Last Post: 23-06-08, 22:58
  4. Tutorial 7 - the packages
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 3
    Last Post: 13-11-07, 01:42
  5. Tutorial Cercasi
    By joyz in forum Actionscript 3.0 base
    Replies: 22
    Last Post: 30-07-07, 09:52

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