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
Everything run by electricity will eventually be connected to the Internet. Get ahead of the Internet of Things revolution and join Akvelon expert and IoT industry leader, Sergey Grebnov, in his session at @ThingsExpo, for an educational dive into the world of managing your home, workplace and all the devices they contain with the power of machine-based AI and intelligent Bot services for a completely streamlined experience.
The financial services market is one of the most data-driven industries in the world, yet it’s bogged down by legacy CPU technologies that simply can’t keep up with the task of querying and visualizing billions of records. In his session at 20th Cloud Expo, Karthik Lalithraj, a Principal Solutions Architect at Kinetica, discussed how the advent of advanced in-database analytics on the GPU makes it possible to run sophisticated data science workloads on the same database that is housing the rich...
IoT is at the core or many Digital Transformation initiatives with the goal of re-inventing a company's business model. We all agree that collecting relevant IoT data will result in massive amounts of data needing to be stored. However, with the rapid development of IoT devices and ongoing business model transformation, we are not able to predict the volume and growth of IoT data. And with the lack of IoT history, traditional methods of IT and infrastructure planning based on the past do not app...
DX World EXPO, LLC., a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, will examine the regulations and provide insight on how it affects technology, challenges the established rules and will usher in new levels of diligence...
In the enterprise today, connected IoT devices are everywhere – both inside and outside corporate environments. The need to identify, manage, control and secure a quickly growing web of connections and outside devices is making the already challenging task of security even more important, and onerous. In his session at @ThingsExpo, Rich Boyer, CISO and Chief Architect for Security at NTT i3, discussed new ways of thinking and the approaches needed to address the emerging challenges of security i...
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...
"The Striim platform is a full end-to-end streaming integration and analytics platform that is middleware that covers a lot of different use cases," explained Steve Wilkes, Founder and CTO at Striim, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
What sort of WebRTC based applications can we expect to see over the next year and beyond? One way to predict development trends is to see what sorts of applications startups are building. In his session at @ThingsExpo, Arin Sime, founder of WebRTC.ventures, discussed the current and likely future trends in WebRTC application development based on real requests for custom applications from real customers, as well as other public sources of information.
SYS-CON Events announced today that Calligo, an innovative cloud service provider offering mid-sized companies the highest levels of data privacy and security, 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. Calligo offers unparalleled application performance guarantees, commercial flexibility and a personalised support service from its globally located cloud plat...
SYS-CON Events announced today that Massive Networks will exhibit 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. Massive Networks mission is simple. To help your business operate seamlessly with fast, reliable, and secure internet and network solutions. Improve your customer's experience with outstanding connections to your cloud.
SYS-CON Events announced today that DXWorldExpo has been named “Global 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. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
SYS-CON Events announced today that Datera, that offers a radically new data management architecture, has been named "Exhibitor" 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. Datera is transforming the traditional datacenter model through modern cloud simplicity. The technology industry is at another major inflection point. The rise of mobile, the Internet of Things, data storage and Big...
While the focus and objectives of IoT initiatives are many and diverse, they all share a few common attributes, and one of those is the network. Commonly, that network includes the Internet, over which there isn't any real control for performance and availability. Or is there? The current state of the art for Big Data analytics, as applied to network telemetry, offers new opportunities for improving and assuring operational integrity. In his session at @ThingsExpo, Jim Frey, Vice President of S...
"We provide IoT solutions. We provide the most compatible solutions for many applications. Our solutions are industry agnostic and also protocol agnostic," explained Richard Han, Head of Sales and Marketing and Engineering at Systena America, in this SYS-CON.tv interview at @ThingsExpo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"DX encompasses the continuing technology revolution, and is addressing society's most important issues throughout the entire $78 trillion 21st-century global economy," said Roger Strukhoff, Conference Chair. "DX World Expo has organized these issues along 10 tracks with more than 150 of the world's top speakers coming to Istanbul to help change the world."
"We are focused on SAP running in the clouds, to make this super easy because we believe in the tremendous value of those powerful worlds - SAP and the cloud," explained Frank Stienhans, CTO of Ocean9, Inc., in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"We've been engaging with a lot of customers including Panasonic, we've been involved with Cisco and now we're working with the U.S. government - the Department of Homeland Security," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"MobiDev is a Ukraine-based software development company. We do mobile development, and we're specialists in that. But we do full stack software development for entrepreneurs, for emerging companies, and for enterprise ventures," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
SYS-CON Events announced today that DXWorldExpo has been named “Global 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. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.