Welcome to Mediasparkles

News : Flash

All properties and methods are public by default. You don't have to declare them public with the public keyword but you can if you want to spell it out. Private properties and methods, however, have to be declared as such.

When a method is public, it means that it is accessible by the original class and by all instances of that class. When a method is private, it is only accessible by the original class. Below is a class with a private sayHello() method.


class Hello {
   private sayHello() {
      trace("hello");
   }
}

If you create an instance of the Hello class and then use that instance to call the sayHello() method, you will get an error.

var greeting:Hello = new Hello();
greeting.sayHello(); // will create an error saying "The member is private and cannot be accessed."

If the sayHello() method was public, you wouldn't get the error. The only class that can access the private sayHello() method is the Hello class itself. Below is an example.

// Hello.as:
class Hello {
   // a private method
   private function sayHello() {
      trace("hello");
   }

   // a public method which accesses the private method
   function sayHelloAnyway() {
      sayHello();
   }
}

//separate.fla
var greeting:Hello = new Hello();
gretting.sayHelloAnyway();

The sayHello() method cannot be accessed from outside of this class, including from class instances. But since the public sayHelloAnyway() method calls the private sayHello() method, sayHello() is indirectly available to class instances. The above code results in a "hello" trace in the output window. It does not cause an error.

Private methods are useful for internal processes that you don't need to or don't want to be exposed to the public. I am sure you can think of much better examples than the one above.

 posted by Vera Fleischer (9/10/2003 11:07:09 PM);




The static keyword is an attribute of a property or a function. If a property is static, it is accessed through the class directly and not through an instance of that class. Most properties are accessed through instances of a class, meaning that they are not static.

Example:

// create an instance of myClass
myInstance = new myClass();

// now access one of its properties
trace(myInstance.property1);


With static properties, however, you use the master class to access them like so:

// access a property through the master class
trace(myClass.property1);


An example with a lot of static properties is the Date class. All of its properties are accessed through the Date class directly. We call, for instance, Date.getUTCMinutes() without first creating an instance of the Date class. All the methods of the Date class are static methods. In other words, static properties and methods are created only once - in the master class - instead of each class member having its own copy.

Of course you can also create your own classes which use static variables or functions. Here is an example:

class Geometry {
static function getDistance(mc1, mc2) {
var distance:Number = Math.sqrt((mc1._x - mc2._x)*(mc1._x - mc2._x)+(mc1._y - mc2._y)*(mc1._y - mc2._y));
return distance;
}
}

Its static getDistance() function returns the distance between two movie clip instances. To call the static method, you would type the following in a separate file.

distance_between_2_dots = Geometry.getDistance(dot1, dot2);


Note that if you were to omit the static attribute and then called the function with the line above, you would get the following AS 2.0 error:
The property being referenced does not have the static attribute.

 posted by Vera Fleischer (9/9/2003 08:06:35 PM);




An ActionScript 2.0 class can either be dynamic or not dynamic. Though it may seem that the alternative to dynamic is static, classes actually can't be static. The static keyword refers to something else which I will get to another day. When a class is dynamic, it is declared like so:


dynamic class someClass {
}


When it is not dynamic, the dynamic keyword is simply left out:

class someClass {
}


Dynamic means that methods and properties can be added at runtime without being declared in the original class. It also means that another class or .fla file can attempt to access methods and properties that don't exist without throwing compiler errors. In a class that isn't dynamic, the same would result in errors.

Below is a simple dynamic class.

dynamic class myClass {
function method1() {
trace("method1");
}
}


Below is some code that implements this class in another file:

var c:myDynamicClass = new myDynamicClass();
c.method1();
c.someMethod();
c.isDynamic = true;


We are calling a method that doesn't exist (someMethod), and we are setting a variable that hasn't been declared (isDynamic). But since the class is dynamic, this is allowed and will not result in ActionScript errors.

If we want to implement a non-dynamic class with the same four lines of code, we would have to write the class slightly differently. Here is how.

class myClass {
function method1() {
trace("method1");
}
// we would have to declare the someMethod method
// so that calling it doesn't cause an error
function someMethod() {
trace("someMethod");
}
// we would have to declare the isDynamic property
// so that accessing it doesn't cause an error
var isDynamic:Boolean;
}


Now the following code will not result in any errors.

var c:myClass = new myClass();
c.method1();
c.someMethod();
c.isDynamic = true;


 posted by Vera Fleischer (9/8/2003 10:37:49 AM);