ActionScript 3.0 Singleton

December 22, 2008

Several singleton implementation were met by me, while surfing through the internet and reading some books. In ActionScript 3.0 Design Patterns there was an implementation, the most met among developers, where the constructor requires some class to get an instance. The fact is that the class required is private for the package where singleton is placed – so no other peace of code can call the constructor. Just because it doesn’t have the class.

public class SomeSingletonClass
{
public function SomeSingletonClass(privateClassRef:Class)
{
if( privateClassRef == PrivateClass )
{
...
}
else
throw new Error("singleton error");
}class PrivateClass ...

I find this method very confusing. The user of the class meets its constructor. And is forced to use singleton get instance method in a very very strange and uncommon way. The desired method would be not a strange requirement but a pretty widely used error of using the constructor (as we know, i’s impossible to make a constructor private in AS3). I can offer a very simple and “close to originals” way of building singleton classes:

public function SingletonClass
{
static public function getInstance():SingletonClass
{
if( _instance == null )
{
_allowCallConstructor = true;
_instance = new SingletonClass();
_allowCallConstructor = false;
}
return _instance;
}

static private var _instance:SingletonClass = null;
static private var _allowCallConstructor:Boolean = false;

Sometimes I need the same method to be a simple method (without having to think what argument to pass) and an event handler at once.

If I have a regular handler

private function init(event:Event):void
{

}

I’d have to pass some sort of argument (probably null would be enough) to call it. If I don’t pass – the warning (or in some compiler mode) an error will appear.

init(null);

If I make just some method, I won’t be able to use it as an event handler error. Since I will get runtime error.

A very light and elegant solution was given in actionscript.org forum.

private function init(event:Event=null);

This prevents from warning when not passing an argument (using as simple method) and both it can be used as an event handler.

public class Main extends Sprite
{
public function Main()
{
if( stage != null )
init();
else
addEventListener(Event.ADDED_TO_STAGE,init);

Flex 3 component lifecycle

October 27, 2008

In a former time, when I only started to learn Flex, it seemed to me a big and crazy mixture of events, methods and properties. I didn’t know which method to call, and how to do this and that. Now flex component instantiation process is not that hard for me, and event much more – it realy seems to be logically structered. I wish I had such a good explanation those times, as done in FlexScript blog. Recommend.

ActionScript 3 namespaces

October 26, 2008

In my previous post I was a bit cinfused with wondering is there is realy some undocumented keyword in flash. The answer was pretty much simple. It was a custom namespace. Probably only a name confused me. Since I had an experience of C++ and Java, the word reminded me good old times. A good post was found about actionscript 3 namespaces.

Today I’ve made my first attempt to control flex component sound. My first ideo was extremely simple:

this.soundTransform.volume = 0.5;

But nothing realy happened. I’ve set the break point in the line of the volume modification and in the line right after that. So in the first breakpoint the value was like 0. But how surprised I wasm when the watch tool showed me the same value in the second line, after the modification. Even despite the factthat volume of SoundTransform class is marked as [read/write].

Next one worked fine in the way I wanted it:

this.soundTransform = new SoundTransform(0.5);

Mistic AS3 keyword

October 20, 2008

How impressed I was, when first met virtual keyword. The keyword actualy was given in a WebOrb code example of using Flex web services. The code was like:

var ro:RemoteObject = new RemoteObject(“GenericDestination”);
ro.SomMethod.addEventListener(ResultEvent.RESULT,onServiceResult);

private virtual function onServiceResult(event:ResultEvent):void

How disappointed I became, when half of an hour of googling didn’t gimme any result for that. My team leader told me that means, that a method can be called from server. Though no documentation is still available for me. Realy mistic…

In my previous post there was a problem inside styleChanged() method. The fact was in that styleProp came with value null to me:(

After several minutes of sleeping on docs, I realized that the value of the property can come null if all the styles (say not one) have changed. So we additionaly check:

override public function styleChanged(styleProp:Strinf):void
{
super.styleChanged(styleProp);

if( styleProp == null )
{
// perform all skin updates
}

}

More to learn on Flex…

Here is the repeated example of adding custom styles to some custom component:

Hi all. I’m trying to make a set of custom styles for component that extends mx:Panel.

skin.css is

.videoPanel
{
backgroundSkin : @Embed[( …
someStyle : 100;
}

Inside mxml file I do:

<mx:Style source=”skin.css”/>
<video:VideoPanel styleName=”videoPanel”/>

And inside VideoPanel.as I have:

[Style(name=”someStyle”,type=”String”,inherit=”no”)]
public class VideoPanel extends Panel
{
private static var classConstructed:Boolean = classConstract();

private static var classConstruct():Boolean
{
if( StyleManager.getStyleDeclaration(“VideoPanel”) == null )
{
var newStyle:CSSStyleDeclaration = new CSSStyleDeclaration();
newStyle.setStyle(“someStyle”,”HelloWorld”)
StyleManager.setStyleDeclaration(“VideoPanel”,newStyle,true);
}
return true;
}

override public function styleChanged(styleProp:String)
{
super.styleChanged(styleProp);
if( styleProp==”someStyle” )
{

}
}


}

The thing is that even if I comment super.styleChanged(styleProp); panel still gets it’s borderSkin. Probably I’ve missed something… But I just followed the official book. Eh… need a vocation!

Any comments about that? Thanks in advance…

PS: How do I get some [CODE] tag equivalent ?