Posted under ActionScript 3.0.

AS3.0 MouseWheel on Mac OS X


I’ve finally found the time to port my SWFObject add-on SWFMacMouseWheel (catchy I know) to ActionScript 3.0 and SWFObject 2.0

There’s now two examples in the zip, one for use with SWFObject 1.5, and a totally re-written version for use with SWFObject 2.0 (formerly SWFFix)

All you have to do in your main application class (or document class) is

1
2
3
import com.pixelbreaker.ui.osx.MacMouseWheel;
 
MacMouseWheel.setup( stage );

You can then add listeners to TextFields, Sprites etc etc as you normally would any other MouseEvent.

Example JavaScript to be used with the dynamic embed method

1
2
3
4
5
6
var vars = {};
var params = { scale:'noScale', salign:'lt', menu:'false' };
var attributes = { id:'testObject', name:'testObject' }; // give an id to the flash object
 
swfobject.embedSWF("test_as3.swf", "flashContent", "100%", "100%", "9.0.0", "js/expressInstall.swf", vars, params, attributes );
swfmacmousewheel.registerObject(attributes.id);

I have updated the SWFObject 2.0 version to be more compact, and it also falls inline with the code style of SWFObject2.0. see swfmacmousewheel_src.js for the uncompressed version, deploy swfmacmousewheel2.js on your site.

Download source and demo here or view the demo online

AS3.0 Memory monitoring


I just came across the property System.totalMemory. It’s very useful, especially for the engine I am currently working on, and dealing with AS3 garbage collection.

It returns the memory currently being used by the Flash Player. It’s in bytes so it pays to clean it up slightly.

[as]
var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + ‘Mb’;
trace( mem ); // eg traces “24.94Mb”
[/as]

It seems this takes account of all instances of the flash player. For example, I have a logger/debug panel running in the sidebar of firefox, built in flex. As soon as I open this and it’s initialised, it uses an extra 5 – 10mb of ram. So be warned, if you see a massive increase in memory usage, check you don’t have loads of sites open with ad banners or other flash based stuff.

Back from Flash on the Beach


photo by BIT-101
I got back from Brighton yesterday, exhausted after 3 days of excellent sessions and some heavy drinking sessions too!

The high points for me were Mario Klingemann’s “2D or not 2D, that is the question” talk about working with BitmapData. He showed how to make a “Magic Wand” tool a la photoshop. His process was impressive to say the least, and I’m very interested in playing with the paletteMap method of the BitmapData class.

The session from André Michelle was amazing, he demoed his PopForge sound synthesis engine. Generating sound entirely within flash at run-time. See the video below, this is all generated and manipulated in flash. It’s simply amazing, I can’t wait to get some time (after this post) to start messing around with sound in flash.

Read more…

AS3.0 JIT vs. Interpreted


I’ve been developing an application recently that requires large numbers of classes to be initialised as soon as the application is loaded. It’s an isometric display engine, a la “Sim City”, with a world map of 800 x 600 tiles, and multiple layers ( terrain, objects, transport networks etc) there’s potential for over 100,000 instances!!!

I found ActionScript 3.0 and AVM2: Performance Tuning by Gary Grossman, which covers many aspects of how AVM2 is different from AVM1. The part I was interested in was about $init and $cinit (class constructor) functions being interpreted and everything else being JIT.

The initial setup of the engine runs over a timed loop (to stop script timeouts), and this was typically taking 20 to 30 seconds. After reading the above PDF, it became clear that class constructors are interpreted, not JIT compiled, so all I had to do was move the code out of the constructor, into an init() function, and call if after the constructor, this shaved a huge amount of time off the initial build of the landscape.

Some initial tests, with a for loop running from 0 to 10000 showed that in the constructor it took about 350ms, and called via another method, it took only 240ms. It is not always faster, but 90% of the time it’s faster.

Read more…

AS3.0 Better Singletons


I’ve found a better more robust way of enforcing singletons in AS3.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package
{
	public class Singleton
	{
		public static var instance:Singleton;
 
		public static function getInstance():Singleton
		{
			if( instance == null ) instance = new Singleton( new SingletonEnforcer() );
			return instance;
		}
 
		public function Singleton( pvt:SingletonEnforcer )
		{
			// init class
		}
	}
}
 
internal class SingletonEnforcer{}

Note: The class “SingletonEnforcer” is actually placed within the same .as file as the Singleton class, but placed outside the package{} parenthesis, therefore, the class “SingletonEnforcer” can only be accessed from within this .as file, so if the Singleton’s constructor is called from anywhere else, you’ll get an error.

AS3.0 Reversed for loop


Today I came across an easy mistake to make when new to AS3.

I set up a for loop, to count backwards through an Array. But for some strange reason the loop was never terminating! It was simple, here’s the original code

[as]
var i:uint;
for( i=arr.length-1; i>=0; i– )
{
}
[/as]

The mistake was simple, I had set i as a uint, so it could never go below 0, so just had to make i an int. It sounds stupid once you realise it, but it wasted enough of my time!

RSS/Atom syndication made easy


A while ago adobe released their as3syndicationlib for easy access to RSS & Atom feeds by converting either RSS 1.0/2.0 or Atom feeds into generic data accessible via the IFeed interface. These libraries also require adobe’s corelib.

It literally took me 5 minutes to write a simple wrapper class to handle the loading of the feeds, and simply trace out the headlines of each item.
Read more…