Welcome to Mediasparkles

News : Flash

Private vs. protected

Since I have gotten some comments on my public vs. private post, I thought I'd investigate a little further.

Simon Wacker mentioned that a private method is no longer private when accessed dynamically. Say that we have a class instance called myClass with a private method called makeMe. Calling the private method directly would result in an error message.


myClass.makeMe(); // results in an error message because the method is private


But calling it dynamically does not result in an error message.


myClass["makeMe"](); // does not result in an error message


This is because the private keyword is a compile time feature only. At compile time, myClass["makeMe"]() is not yet resolved to mean myClass.makeMe(), so the calling of a private method is never caught.

Darron Schall pointed out that the private keyword in ActionScript 2.0 works the way the protected keyword works in other languages. I did some research to find out what the difference is between private and protected in other languages. Here is the answer I found:

Private members are accessible by member functions.
Protected members are accessible by member functions and derived classes.


What does this mean? It means that private methods can only be used by the class that owns the methods. Protected methods, on the other hand, can be used by the class that owns the methods and by any of its subclasses.

It is true that in AS 2.0 a subclass can access a private method of its parent class.


// baseClass.as
class baseClass {
   private function privateMethod() {
      trace("you have called a private method");
   }
}

// derivedClass.as
class derivedClass extends baseClass {
   function tryPrivateMethodFromBaseClass () {
      privateMethod();
   }
}

// .fla that implements these classes
var myClass:derivedClass = new derivedClass();
myClass.tryPrivateMethodFromBaseClass(); // does not result in an error message


In the above code, I am creating a new class called derivedClass which is a subclass of baseClass. In one of the subclass's methods I then access the parent class's private method. It lets me. This is proof that the private keyword really does act like the protected keyword. If the private method was truly private, it would not be available to the subclass.

Lastly, Peter Hall mentioned that private methods are accessible by other instances of the same class. I tried to see what that's all about.


// someClass.as
class someClass {
   // this variable will reference the other instance of the same class
   var sisterInstance:Object;
   private function privateMethod() {
      trace("you have called a private method");
   }
}

// .fla
// create two instances of the same class
var myClass1:someClass = new someClass();
var myClass2:someClass = new someClass();

// assign a reference to one instance to the other instance
myClass2.sisterInstance = myClass1;

// the following results in an error message
myClass1.privateMethod();
// this, however, does not
myClass2.sisterInstance.privateMethod();


That's fascinating. myClass1.privateMethod() throws a compile time error while myClass2.sisterInstance.privateMethod() does not, even though myClass2.sisterInstance resolves to myClass1, and thus the statements say the same thing. So this is yet another abnormality of the AS 2.0 private keyword. Don't you love how unique ActionScript is as compared to other languages? I do!

 posted by Vera (9/30/2003 07:16:00 PM);




Are you going to Flash Parade?

I'm thinking about going, especially now that you can get a 25% discount via actionscripthero.com. The discount code is pFP975111 and you can use it whenever you want. You can also pass this code on to your friends.

One of the reasons why I am thinking about attending is that I would love to meet some of you European Flashers! Let me know if you're going.

 posted by Vera (9/28/2003 09:52:00 AM);




Lunch with Mike Chambers

Today I had lunch with Mike Chambers. As many of you know, Mike has finally made the move out to San Francisco, and we are very glad to have him around. What do two Flash developer-turned-Macromedians talk about? Let's see... We talked about our Flash initiation versions (3 for him, 4 for me), our startlingly non-technical Economics backgrounds, Central, Flash Forward, Northern Virginia, the Flash blogosphere, the Flash community, and about YOU. Yes, that's right. Depending on who is reading, you may have come up in our conversation. Don't put it past us.

 posted by Vera (9/19/2003 05:55:00 PM);




Cubicle art

A few friends from work and I are doing a photography contest with the theme Cube, as in cubicle. Here are my submissions.


A collection of the more colorful contents of my cube


My gym bag


A printed out copy of the component hierarchy. If you want your own copy for your own wall, let Peter Hall tell you where to get it.

 posted by Vera (9/19/2003 07:26:00 AM);




RSSify TNG

I know most of you Flash bloggers use Movable Type, but just in case you don't and are in need of a RSS feed, you might check out RSSify TNG. It was recently written by Alp Uckan from Köln, Germany. But even if you don't speak German, have no fear because I translated all the docs into English for you! The cool thing about this RSSifier is that you get all kinds of options, such as RSS 0.92/1.0/2.0, keep/drop HTML, which link to put in the <LINK> tag, Funky/Unfunky, etc. Take a look at the demo for details. I have recently migrated my own feed to RSSify TNG, and my life has been much better since.

 posted by Vera (9/17/2003 07:26:00 PM);




I am a hero!

I got the following email today:

We are very pleased to announce that you have been selected as :

Vera Fleischer actionScript Super Hero
ACTIVE MEMBER of the Hall of Justhese
Flash based aggregator. Paris -- France.
Categories: actionScript Heroes AND macromedia blogs
http://www.actionscripthero.com/rssViewer

Wow. I have no idea what I did to deserve to be listed on a site that aggregates the "amazing adventures of the most famous actionScript Heroes in the World." But ya-ay. And listen to that great majestic sound when you first come to the page (turn up your speakers).

Thanks for the honor, aSH!

 posted by Vera (9/16/2003 11:03:00 PM);




Flashextensibility.com

A new site all about the JavaScript API is up and running: Flashextensibility.com. It is run by Keith Peters and Todd Yard and supplements their Friends of Ed book titled Extending Flash MX 2004, Complete Guide and Reference, which is due out in November. The site has a forum, downloads, and more. Go get involved!

 posted by Vera (9/15/2003 07:52:00 AM);




MX Developer's Journal

It looks like I wasn't the only one complaining about the lack of Flash in the print media. Enter the MX Developer's Journal. I'm so subscribing. Are you? (via Peldi's Little Blog)

 posted by Vera (9/14/2003 10:11:00 PM);




MM bloggers

I met three blogging Macromedians within one week. On Monday I met Mike Chambers. He was standing in front of one of the Macromedia buildings waiting for his ride, and I walked up to him and introduced myself. On Friday I met JD. He was at a meeting in my building and came to my cube for a chat afterwards. Then today I met Peldi at the park! I was there hooping with my friends, and he was there playing cricket with his friends. That's three in one week! Incredible.

 posted by Vera (9/14/2003 10:02:00 PM);




AS 2.0: public vs. private

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 (9/10/2003 11:07:00 PM);




AS 2.0: The static keyword

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 (9/09/2003 08:06:00 PM);




AS 2.0: The dynamic keyword

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 (9/08/2003 10:37:00 AM);




Including an external method in ActionScript 2.0

After my post about AS 2.0 last week, Kristin asked in the comments how to include methods that live in external files. I replied to her that in order to give a class a certain method, you had to either extend a class that owns that method or define the method in the class itself. But I was wrong. You can easily include methods from external files. Here is how. Say you have a file called ExternalMethods.as. This file, for our purposes, contains nothing but this:


function sayGoodbye() {
trace("Goodbye World.");
}


You can then include this method in the HelloWorld class like so:


class HelloWorld {

// the method that says hello
function sayHello() {
trace("Hello World.");
}
// include a method defined in a separate file
#include "ExternalMethods.as"
}


The sayGoodbye() method is now a method of the HelloWorld class just like the sayHello() method is.

 posted by Vera (9/04/2003 07:16:00 PM);




Which MX 2004 number are you?

I just found out via Sean Voisen that you will get a free T-shirt when you preorder MX 2004. Each T-shirt will have a number on it, reflecting your spot in the line. Who is going to be first in line? I am all for putting up a photo gallery of people wearing their numbered T-shirts. If you want to be included, please email me a picture of yourself proudly showing off your number.

 posted by Vera (9/02/2003 10:55:00 AM);