ActionScript 3 tips and code snippets

As we all know, human brain is not perfect. It's hard to remember everything, sometimes it's just easier to keep all the things learnt in one place and then come there later if necessary. This is why I decided to write some of my notes and snippets here. Please feel free to use it and comment if you have any advices or corrections :)  Enjoy!

 
May
20
2010

Using the new Vector Class in place of the Arrays

Attention: open in a new window. Print

Vectors are one the new "features" of the Flash Player 10. Unlike the Arrays, they can only keep one type of data. So trying to add a String to a Vector that was initialised to contain only Integers will result in a compile error. The main advantage of using the Vector class is increased performance, as Flash doesn't have to check its content to verify what kind of data is stored there. It also provides a type safety, if you try to store multiple types of data it will give you an error (well, not always as you will see in the article).

   
Mar
18
2010

Native 3D transformations issues: objects get blurry, slightly bigger and have wrong depth

Attention: open in a new window. Print

Starting from Flash Player version 10 we have access to the new 3D capabilities. First of all Flash doesn't offer full 3d support, this is only 2.5d (or postcard in space). You can't create rotating boxes, but you can operate flat movie clips in the 3-dimensional space. Anyway, I decided to give it a try and create simple 3d carousel banner that will nicely fit to company's website. I was inspired by http://openscreenproject.org website, Adobe's initiative to support "our hard work" on all mobile devices possible (hello Mr Jobs...). This seemingly easy task turned out to be quite tricky and required few "workarounds" to achieve desired effect. As soon as I applied 3d transformation, movie clips got blurred and grown one pixel! Also objects that supposed to be in the front of other objects as per the z - order, were hidden in the back as per the Display List order.

   
Mar
03
2010

Flash on top of the HTML elements - changing the window mode parameter

Attention: open in a new window. Print

Developers are not aware of the problem with a flash content "always on top" until they embed it in a HTML that has the elements that meant to be in front of everything else. This usually happens when using the CSS & java-script based drop-down menus. Embedding your movie with default settings always place it "outside" the HTML layers hierarchy and the z-index ordering.

   
Feb
17
2010

Changing the right click menu content in Flash with ContextMenu object

Attention: open in a new window. Print

You have probably noticed many times that some Flash websites have customised right click menus. Usually Flash developers remove standard options like Zoom In and Out, Show All and Quality settings. But you can also add your own options and make them interact with your project.

   
Jan
25
2010

Lab: Autopsy of Skinner's collision detection in AS3

Attention: open in a new window. Print

In this article I'm working out the famous Grant Skinner's collision detection algorithm (yes, I know... I don't have any more interesting things to do at the moment :-) ). I will go through the code, line by line, trying to understand the whole process.

   
Jan
19
2010

Collision detection methods, hitTest and hitTestObject alternatives

Attention: open in a new window. Print

Flash has never had a perfect solution to detect collisions. HitTest from ActionScript 2 has been replaced by two separate methods in ActionScript 3: hitTestObject and hitTestPoint. They are very similar to their old brother... they are still not perfect. Fortunately there are few alternative collision detection methods available, I'm going to test them here in this article.

   
Jan
10
2010

Bit shifting and masking, bitwise operators in AS 3. Getting Alpha channel out of the color.

Attention: open in a new window. Print

In this article I'm going to show how to extract color channels out from the color value and how to create new colors using bitwise operators in Action Script. First there is some theory with examples and then practising with coding.

   
Nov
21
2009

Flash CS4 slow and unresponsive

Attention: open in a new window. Print

I have recently upgraded my operating system to the brand new Windows 7. After installation of Creative Suite 4 (when there was nothing else installed except Antivirus and Winamp) I started Flash as a first application to continue working on one of my projects. Sadly I noticed that everything is extremely slow, even when typing code, the editor became just unresponsive and came back to life after few seconds... Everytime when clicking on any of the buttons from Flash panels, there was about 2-3 seconds delay.

   
Oct
19
2009

TIP: Setting the class path permanently for all Flash projects

Attention: open in a new window. Print

When working with classes, you need to show Flash where to look for them. In order to do that you need to define a class path – which points to the root of the whole organized structure of directories and subdirectories containing your different classes (packages). I used to define a document-level class path – separate for every new Flash project. But there is a way of creating a global class path for all the projects you will work on.

   
Jun
23
2009

Passing values from website URL (GET method) to Flash

Attention: open in a new window. Print

There are plenty of situations when you may need to pass arguments from URL to the Flash. In my case I was looking for a way to change the Flash banner on a corporate website to display different content for different marketing campaigns. Company runs number of different campaigns for different products, publishing many ads on IT portals. The Flash banner on company's website will display a different advert according to the parameter in URL that will be passed from these ads. (like http://company.com/index.php?campaign=5).

   
Jun
06
2009

Incorrect stage.stageHeight value when testing a movieClip

Attention: open in a new window. Print

This is a small annoying bug present in Flash 8, CS3 and CS4. If you create a movie, lets say 550x400 px and add a trace command to your actions: trace(stage.stageHeight); you will get an incorrect result like 300, not 400px;

   
Jun
05
2009

Using XML in ActionScript 3 with E4X

Attention: open in a new window. Print

XML is an industry standard for data exchange between various applications written in different development languages and running on different operating systems. It allows describing complex and hierarchical data in simple and logical terms. ActionScript 3 comes with E4X (ECMAScript for XML) which is a programming language extension that adds simpler and easier to read approach for working with XML. In this article I concentrate on how to load, parse and modify XML data from Flash.

   
May
14
2009

Setting the tab order of the form items and customising the focus rectangle

Attention: open in a new window. Print

The tab order is the sequence in which a user moves focus from one object to another by pressing the TAB key. By default, the tab order is the same as the order in which you created the objects. So, if lets say you created the submit button first and then the input boxes, then the button will have tab focus first before input boxes. This can be changed from Action Script using the tabIndex property of every display object.

   
Apr
20
2009

Using filters from the ActionScript level

Attention: open in a new window. Print

To apply any filter from the ActionScript 3 we use the "filters" property of every display object. All the filters are available from the flash.filters package. The example below presents how simple is to apply a filter to the MovieClip instance.

   
Apr
19
2009

The "Full browser screen" Flash project with tweening

Attention: open in a new window. Print

"Full screen" or "full browser screen" Flash websites became a standard on the web now. And it's actually very easy to create such a website as ActionScripts 3 provides us with RESIZE event to track size changes of the Flash Player window or the internet browser. Actually, all we need to do is just adjust x and y properties of our display object or resize any background to fit the resized window.

   
Mar
25
2009

Colors in Action Script. What is the alpha channel value of transparent pixels?

Attention: open in a new window. Print

ActionScript uses a 32-bit hexadecimal numbers...

   
Feb
28
2009

Two ways of preloading in ActionScript 3

Attention: open in a new window. Print

Preloader is one of these things that are essential when building a Flash project that will be published on the internet. Firstly, it prevents from playing before all its content is loaded. Without it, animation could start without somactioe of the movie clips on the scene. Secondly, it informs users how long they need to wait before downloading is complete, so they either can close the browser and search for some other content that loads faster (the worst case scenario Laughing) or they can wait few more seconds to experience your great work. There are two ways of preloading your content, you can create "internal" preloader for a movie clip itself (should I call that selfpreloader?) or you can create one swf file - loader that will load another swf file - your main project.

 

   
Jan
05
2009

Example of a recursive function to trace the display objects

Attention: open in a new window. Print

To use this function, just pass your display object or stage reference to get the list of the all display objects sorted in a tree view.

   
Nov
23
2008

How to create a custom Event that will also pass an argument?

Attention: open in a new window. Print

The most convenient way to communicate between objects is to use events. I'll show an example how to create a custom event that will also carry a parameter.

   
Oct
26
2008

How to change a tint of the Movie Clip from ActionScript?

Attention: open in a new window. Print

Applying a tint from ActionScript is almost as easy as from Flash GUI. All we need to do is to customise setTint property of the Color object and then apply it to the Movie Clip's transform property.

   
Oct
10
2008

getURL in ActionScript 3

Attention: open in a new window. Print

There is a little more to write when you want to link from Flash to some website using ActionScript 3. ActionScript 2 offered the getURL method, ActionScript 3 doesn't have it anymore.

   
Sep
07
2008

What's the best practice to use keyboard in your Flash games?

Attention: open in a new window. Print

To capture which key was pressed by a user, we use two keyboardEvents - KEY_UP and KEY_DOWN. When a user presses the key down, KEY_DOWN event is dispatched only once. But if you want to create a game where to make a hero walking or a car driving  user needs to keep a key down for a longer time. Obviously you don't want a user keeps pressing key time after time.

   
Jul
26
2008

Getting a list of frames and frame names in the Movie Clip

Attention: open in a new window. Print

The MovieClip object has a property called currentLabel that returs an array. Iteratntg through the table we can get frame numbers from .frame and frame names from .name property.

   
Jun
09
2008

Loading an external text file with ActionScript 3

Attention: open in a new window. Print

To load a text file with ActionScript, you only need few lines of code. I'm gonna present both, a short way and a valid way of doing it.

   
May
05
2008

Using an anonymous inline function when adding an event listener

Attention: open in a new window. Print

I show here how to use an inline event handler function when creating an event listener and how to remove it later.

   
Apr
30
2008

Getting a reference of a tweened MovieClip from an event handler function

Attention: open in a new window. Print

When you are tweening an object using built-in Tween classes and you want to perform some actions on it when the tweening is done, you can reference the object using TweenEvent's obj property.

   
Apr
28
2008

Ways of creating and reading an associative array

Attention: open in a new window. Print

In this short article I'm exploring ways of creating and reading object or an associative array.

   
Apr
26
2008

Different ways of creating an Array

Attention: open in a new window. Print

There are number of ways of creating an array, depending on how you are going to use it in your project...

   
Apr
20
2008

Shorthand conditional statement (if)

Attention: open in a new window. Print

Knowing the shorthand notation can save you time and lines of code. I present here two examples of short version of "if" conditional statement.

   

AS3 Tips

I'm with Adobe - Facebook group