Welcome!

Adobe Flex Authors: Matthew Lobas, PR.com Newswire, Shelly Palmer, Kevin Benedict

Related Topics: Adobe Flex

Adobe Flex: Article

Flash MX Programmer's Guide to ActionScript 2.0

The more tools you have, the more you can build

Flash developers who are still using ActionScript 1.0 are drastically limiting their capabilities, wasting valuable time and resources. Because most Flash developers come from a nonprogramming background, they don't fully understand the true power and benefits of ActionScript 2.0. This article attempts to clear up some common misconceptions about AS2 and gives some clear examples demonstrating the new concepts and methodologies that can be applied using it.

AS1 Is Not Deprecated
What is now referred to as AS1 is the same AS you'll find in Flash MX. Contrary to popular belief, the AS1 programming style - using prototype objects as classes - is not deprecated. You can still write prototype objects and modify existing objects' prototypes in AS2 using the exact same AS1 syntax. AS2 does not deprecate AS1; it extends and refines it.

Class Declarations
With AS1 you could only use prototype objects to define a class (see Code I).

For component development you would also have to use the #initclip and #endinitclip pragmas as well as the Object.registerClass method to assign your class to a MovieClip in the library.

With AS2 you could still write the same prototype object or you could write a true class. To write a class you need to define your class in a separate AS file much as you would in Flash MX for external AS. For classes, the AS file must have the same name as the class itself. Some people might find this to be a hindrance; however, this requirement actually helps you better organize and structure your code (see Code II).

The prototype object and the class example do the exact same thing. For comparison, the constructor function was left in the example in Code II. However, a class does not explicitly have to have a constructor function defined as it does for prototype objects. In addition, no other code is required for component development. You can simply specify which class a MovieClip should use in the MovieClip's Linkage Properties panel found in the library.

When compared side-by-side you can see that the class definition is actually cleaner looking and is less redundant. In the prototype example, for every method of a class you must type the name of the object you're adding the method to and the location of the method (specified by prototype).

Some might argue that despite the redundancy the prototype approach is actually more flexible because you can add a method to any class dynamically at anytime in AS1. (This is known as delegation.) Again, AS2 doesn't deprecate AS1, so you can still reference an object's prototype even inside a class definition (see Code III).

Properties
In AS1 you could add a property to a prototype object by first declaring a method to be used as a getter and then declaring another method to be used as a setter. Then you had to use an addProperty method on the prototype object to wire the getter and setter methods together as one property (see Code IV).

You can still use the exact same syntax in AS2 or you can just declare a getter and setter function (see Code V).

In the example in Code V, Flash MX 2004 Pro does all the work of the addProperty method for you and automatically ties the getter and setter methods together in a property named "_prop". Note that this is no longer needed in most cases, saving you some extra typing.

Static, Public, Private
In AS1 there was no supported way of specifying a public, private, or static method. However, with AS2 this is possible (see Code VI).

A public method simply means that an outside object is allowed to directly access the specified property or method. A private method or property may only be accessed internally from within the class. Static methods or properties are similar to private methods in that they may only be called from within the class itself. However, static methods or properties are created only once per class rather than being created in every object based on that class.

Strict Data Typing
Another new feature of AS2 is Strict Data Typing (aka Type Casting). This allows you to cast (assign) a specific data type or class to a variable. It is especially useful when debugging. Say, for example, in AS1 you have a prototype object called List that has a method called "addNewValue":

var MyList = new List();
MyList.addNewVlaue(1);

However, when you test the movie the value never gets added to the List and you are forced to figure out the problem on your own. Granted, a typo like this by itself is pretty easy to find. However, buried in a couple thousand lines of code it may not be as obvouis.

Strict Data Typing deals with this problem by allowing only public methods or properties of nondynamic classes to be called (see Code VII).

Case Sensitivity and Member Variables
Another new addition to AS2 is case sensitivity. This means that a variable called "someVar" would have a different memory location than "somevar" would. This allows each variable to exist without one overwriting the other. In addition, gone are the days of declaring an instance/member variable anywhere inside of a prototype object as in Code VIII.

In AS2 classes you have to declare all your member variables first (see Code IX). Some developers find this to be an inconvenience initially (especially when converting old AS1 code to AS2). However, over time this will help make you a more consistent programmer and will make your code easier to read and maintain. Note the use of m_ in front of someVar. This is just a naming convention I've adapted in my source code to specify a private member variable.

Conclusion
With all the features of AS1 and the many new additions to AS2, most of which I didn't even get a chance to cover in this article, Flash developers refusing to use AS2 are limiting the tools and capabilities they have for completing a job. You wouldn't want to build a house with just a hammer. A good developer knows, just like any construction worker, that the more tools you have in your tool box, the more things you can potentially build.

More Stories By Erik Bianchi

Erik Bianchi, a member of the editorial board of Web Designer's & Developer's Journal, is a software engineer with more than five years of experience developing Flash-based RIAs and enterprise-wide applications for Fortune 50 and 500 companies. In his spare time he enjoys building Flash-based games, writing or tech editing Flash-related books, and when burned out on code, playing video games on his PC/console systems. You can get more info about Erik and his latest projects on his blog at www.erikbianchi.com.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Erik Bianchi 03/17/04 03:28:43 PM EST

Obvious was intentionally misspelled to demonstrate how easy it is to overlook a typo. Many people didn't notice despite the bolding. Probably should have put a note in there or something to clarify. =)

Thanks for your post and sorry for the confusion.

-erik

A. MacDonald 03/17/04 10:14:49 AM EST

spelling mistake in news - it was pretty OBVIOUS

var MyList = new List();
MyList.addNewVlaue(1);

However, when you test the movie the value never gets added to the List and you are forced to figure out the problem on your own. Granted, a typo like this by itself is pretty easy to find. However, buried in a couple thousand lines of code it may not be as >>> obvouis. << ERROR <<

@ThingsExpo Stories
What is the best strategy for selecting the right offshore company for your business? In his session at 21st Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, will discuss the things to look for - positive and negative - in evaluating your options. He will also discuss how to maximize productivity with your offshore developers. Before you start your search, clearly understand your business needs and how that impacts software choices.
SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp em...
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
While some developers care passionately about how data centers and clouds are architected, for most, it is only the end result that matters. To the majority of companies, technology exists to solve a business problem, and only delivers value when it is solving that problem. 2017 brings the mainstream adoption of containers for production workloads. In his session at 21st Cloud Expo, Ben McCormack, VP of Operations at Evernote, will discuss how data centers of the future will be managed, how th...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...
SYS-CON Events announced today that SourceForge has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. SourceForge is the largest, most trusted destination for Open Source Software development, collaboration, discovery and download on the web serving over 32 million viewers, 150 million downloads and over 460,000 active development projects each and every month.
SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...