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
SYS-CON Events announced today that Outscale, a global pure play Infrastructure as a Service provider and strategic partner of Dassault Systèmes, will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Founded in 2010, Outscale simplifies infrastructure complexities and boosts the business agility of its customers. Outscale delivers a secure, reliable and industrial strength solution for its customers, which in...
As cloud adoption continues to transform business, today's global enterprises are challenged with managing a growing amount of information living outside of the data center. The rapid adoption of IoT and increasingly mobile workforce are exacerbating the problem. Ensuring secure data sharing and efficient backup poses capacity and bandwidth considerations as well as policy and regulatory compliance issues.
SYS-CON Events announced today that Cloud Academy will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud computing technologies. Ge...
SYS-CON Events announced today that Interoute has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Interoute is the owner operator of Europe's largest network and a global cloud services platform, which encompasses over 70,000 km of lit fiber, 15 data centers, 17 virtual data centers and 33 colocation centers, with connections to 195 additional partner data centers. Our full-service Unifie...
SYS-CON Events announced today that delaPlex will exhibit at SYS-CON's @ThingsExpo, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. delaPlex pioneered Software Development as a Service (SDaaS), which provides scalable resources to build, test, and deploy software. It’s a fast and more reliable way to develop a new product or expand your in-house team.
In his opening keynote at 20th Cloud Expo, Michael Maximilien, Research Scientist, Architect, and Engineer at IBM, will motivate why realizing the full potential of the cloud and social data requires artificial intelligence. By mixing Cloud Foundry and the rich set of Watson services, IBM's Bluemix is the best cloud operating system for enterprises today, providing rapid development and deployment of applications that can take advantage of the rich catalog of Watson services to help drive insigh...
The 21st International Cloud Expo has announced that its Call for Papers is open. Cloud Expo, to be held October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, brings together Cloud Computing, Big Data, Internet of Things, DevOps, Digital Transformation, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with the 21st International Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. @ThingsExpo Silicon Valley Call for Papers is now open.
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assis...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend @CloudExpo | @ThingsExpo, June 6-8, 2017, at the Javits Center in New York City, NY and October 31 - November 2, 2017, Santa Clara Convention Center, CA. Learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
In order to meet the rapidly changing demands of today’s customers, companies are continually forced to redefine their business strategies in order to meet these needs, stay relevant and continue to see profitable growth. IoT deployment and development is integral in this transformation, and today businesses are increasingly seeing the value of investing their resources into IoT deployments. These technologies are able increase ROI through projects such as connecting supply chains or enabling sm...
DevOps is often described as a combination of technology and culture. Without both, DevOps isn't complete. However, applying the culture to outdated technology is a recipe for disaster; as response times grow and connections between teams are delayed by technology, the culture will die. A Nutanix Enterprise Cloud has many benefits that provide the needed base for a true DevOps paradigm.
SYS-CON Events announced today that Progress, a global leader in application development, has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Enterprises today are rapidly adopting the cloud, while continuing to retain business-critical/sensitive data inside the firewall. This is creating two separate data silos – one inside the firewall and the other outside the firewall. Cloud ISVs ofte...
SYS-CON Events announced today that Tintri, Inc, a leading provider of enterprise cloud infrastructure, will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Tintri offers an enterprise cloud platform built with public cloud-like web services and RESTful APIs. Organizations use Tintri all-flash storage with scale-out and automation as a foundation for their own clouds – to build agile development environments...
SYS-CON Events announced today that DivvyCloud will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. DivvyCloud software enables organizations to achieve their cloud computing goals by simplifying and automating security, compliance and cost optimization of public and private cloud infrastructure. Using DivvyCloud, customers can leverage programmatic Bots to identify and remediate common cloud problems in rea...
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
SYS-CON Events announced today that Cloudistics, an on-premises cloud computing company, has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Cloudistics delivers a complete public cloud experience with composable on-premises infrastructures to medium and large enterprises. Its software-defined technology natively converges network, storage, compute, virtualization, and management into a ...
Every successful software product evolves from an idea to an enterprise system. Notably, the same way is passed by the product owner's company. In his session at 20th Cloud Expo, Oleg Lola, CEO of MobiDev, will provide a generalized overview of the evolution of a software product, the product owner, the needs that arise at various stages of this process, and the value brought by a software development partner to the product owner as a response to these needs.
SYS-CON Events announced today that Tappest will exhibit MooseFS at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. MooseFS is a breakthrough concept in the storage industry. It allows you to secure stored data with either duplication or erasure coding using any server. The newest – 4.0 version of the software enables users to maintain the redundancy level with even 50% less hard drive space required. The software func...
New competitors, disruptive technologies, and growing expectations are pushing every business to both adopt and deliver new digital services. This ‘Digital Transformation’ demands rapid delivery and continuous iteration of new competitive services via multiple channels, which in turn demands new service delivery techniques – including DevOps. In this power panel at @DevOpsSummit 20th Cloud Expo, moderated by DevOps Conference Co-Chair Andi Mann, panelists will examine how DevOps helps to meet th...