Archive for October, 2007

AS3 : Display programming and Event Listeners

October 26, 2007

My study of “Essential Actionscript 3.0 ” continues…

To reduce memory and processor usage, event listeners should always be unregistered when they are no longer needed in a program.
For eventListeners registered to the ancestor of the display objects :
event.currentTarget : refers to the display object’s ancestor
event.target : refers to the actual object,which trigered the event

Dispatching a new custom event in Actionscript is as simple as extending the EventDispatcher class, giving the new event a name, and invoking the EventDispatcher class’s instance method dispatchEvent();
The object on which dispatchEvent() is invoked is the event target.

Display objects :
addChild() : adds a display object to the Application’s Main swf ( displayObj.root returns the Main Application Object )
stage.addChild(): adds a display object to the stage ( displayObj.root returns the Stage Object )

AS3 : OOP thought process [ inheritence, composition, type casting, Null, void ]

October 24, 2007

My study of “Essential Actionscript 3.0 ” continues…

final :
1. no inheritence
2. no override
3. executes faster ( future version of flash player may execute “final” and “non-final” methods at same speed )
4. hides a class’s internal detail

Note : There is a major opinion difference exists, between developers, whether to make things “final” or not on the basis of basic OOP priniciple.
[OOP says : An instance of a subclass can be used anywhere an instance of its super class is expected.]

Relationships : is-A, has-A, uses-A

Composition is favoured over Inheritence as an application design strategy.

1.Changing code that uses composition has fewer consequences than changing code that uses inheritence.
want to use service of another class : use composition
2. Code based on inheritence often executes faster than code based on composition.
want to use polymorphism : consider inheritence

DataType : Automatic runtime-conversion is known as “coercion” (conversion).
as : “Date” and “Array” type casting is done with “as” operator ( myVar as Array; ) rather than usual type casting like “dataType(Expression);”
is : “is” is used to check the datatype of an Object. ( if( msg is String ) )

“Null” and “void” are data types
“null” and “undefined” are values
“void” data type objects has got one and only value “undefined”.
“Null” data type objects has got one and only value “null”.

“null” value represents the absence of data with a specified type annotation set to anything but Boolean,int,uint,Number.
“undefined” value represents the absence of data without a specified type annotation. “undefined” value also represents the complete absence of a variable or method on an object whose class is defined as “dynamic”.

Function ( Function closure )

October 22, 2007

My study of “Essential Actionscript 3.0 ” continues…

Types of functions depending upon its visibility :

1. Method level ( nested functions ) [ defined inside a method ]
2. Source File level [ defined outside the package definition in a ".as" file ]
3. Package level [ defined outside the class definition but inside the package definition in a ".as" file ]
4. Global level [ defined inside an unnamed package definition ]

Mostly, private static methods are used instead of “Source File level” functions.

Recursive function calls are less efficient than loop iterations.

Default recursion limit is 1000.

Static variables and methods

October 22, 2007

Well, my reading of “Essential Actionscript 3.0″ continues. Lets see whether we are going “Static” or not.

Static and Instance variable of the same name can co-exist in a class. In order to access the static variable, one must write the class name before the variable name ( myClass.PROPERTY ).Only the variable name by itself will refer to the instance variable. Therefore, Instance variable is said to “shadow” the Static variable.
By convention, Static variables are declared before Instance variables in a class.

Static methods can not use “this” key word.
Static methods can not access “Instance” variables and methods.

Actionscript automatically creates one Static method, known as “class initializer” for every class. Code in the class initializer runs in interprited mode, and is not compiled by JIT compiler. Because JIT compiled code generally executes much more quickly than interpreted  code, one should move all the processor-intencive code out of the class initializer when performance is a priority.

Every class in Actionscript is represented at runtime by an instance of the “Class” class.

New settings in “Publish Settings…”

October 9, 2007

There are some tricky settings come with FlashCS3, which may cause a lot of frustration, if not used properly. As Flash now supports a document level “Class”, one can assign a “Class” to whole of the “Fla”. This class is the point of entry to the application one might be developing. Lets try a simple code here.

doc.JPG
Create an “FLA” and name it “class.fla” ( name is not important, you can name it whatever you want ). Now open the properties pannel for the “FLA” and assign a “Class” named “Mclass” for “Document class”. Create a “.as” file in the same folder as the FLA and name it “Mclass.as”. Lets create a movieclip in our FLA.Drag the Movieclip from Library to Stage so that there would be an instance of the movieclip on stage. Name the instance as “my_mc” on stage.Open “Mclass.as” and put the code below
package {
import flash.display.MovieClip;
public class Mclass extends MovieClip{
public var my_mc:MovieClip;
public function Mclass(){
trace(’sam : ‘+my_mc.name);
}
}
}

Now if you test your movie, it should run fine with the trace. Is it running fine?!! If yes, you do not know about something which is acting currently in your favour. If the movie is not running fine ( “Compiler Errors” pannel is showing some error messages ), you must search about that something which is not working in favour of you.We, will see that in a minute.Before that lets test our movie with a little modified code and see what is happening.Put the code below instead of the previous code in “Mclass.as”.
package {
import flash.display.MovieClip;
public class Mclass extends MovieClip{
public function Mclass(){
trace(’sam : ‘+my_mc.name);
}
}
}

If you test movie now, for those who felt the luck was not in favour, will suddenly feel that everything is working fine and luck is with them.But there would be dissapointment for those who felt that their movie was working fine previously.Now they should get an error in “Compiler Errors” pannel.
Well, well, the culprit is the publish settings.Lets have a look at it.Open the publish setting dialogue box by going through “File > Publish Settings…”. Now click the “Settings…” button next to “ActionScript version” drop down list (By the way, your “ActionScript version” setting should be set to “ActionScript3.0″). This should open “ActionScript 3.0 Settings” dialogue box. There is a checkbox just next to “Stage” and before “Automatically declare stage instances“. This is the setting, one must keep an eye on.

setting.JPG
What does it do?
When this checkbox is selected, Flash automatically declares “named instances on Stage”.That means in a Document class, there should not be any declaration for the named instances on stage as
public var my_mc:MovieClip;
When this check box is not selected, one must declare the “named instances on Stage”. One must declare them in the class before using it.
Now, if we remove the instance name from the properties panel, we can safely test our movie without any error, thats because anyway we can not access that movieclip.

So, for those of you who have “Automatically declare stage instances” setting on, the second bunch of code will work and for those who have “Automatically declare stage instances” setting off, the first bunch of code will work. You can see, there is only one line difference between both the codes and that is declaration of the movieclip.

Happy Flashing :)

Adobe MAX Chicago. WOW :)

October 3, 2007

First of all I must thank Aral Balkan for sharing the Adobe MAX videos. This seems to be Flash has gone a long way and will go a long long way the way Adobe is preparing it. Till now in the second day of the conference there is already new announcements made and that were as below.

1. Flash Palyer 10 ( codename : ASTRO )
2. New Bitmap Effect scripting language ( codename : HYDRA )
3. A new designers’ tool for RIA ( codename : THERMO )

Here is the link to Aral’s blog, where you can feel as if you are in MAX itself. :)