AS3 Proxy Example

Some people have problems with the migration from AS2 to AS3. First of all. This post will not help you to migrate. Also using the stuff here is bad. I just thought I can share some idea I had and make nice use of the Proxy class you have with AS3. Why is it bad? It will make your project run slower.

First of all. What can you do with Proxy? The proxy is defined as a dynamic class and it allows you to override any method and also the “delete” operator for instance. Back to the problem: Someone said “why can I not use clip0.clip1.clip2” any longer. Another friend told me it took him some time to figure out he has to use getChildByName( 'clip0' ).getChildByName( 'clip1' ).getChildByName( 'clip2' ). By making use of the Proxy class we can give this possibility back to the users.

Since I am a nice guy I added also _x, _y, _xscale and _yscale. Now how to do that? You define a proxy that holds a Sprite object (actually… not really. More on that later). Then you have to override a lot of methods like this:

[as]flash_proxy override function getProperty( name: * ): *
{
if ( sprite.hasOwnProperty( name.toString() ) )
{
return sprite[ name.toString() ];
}
else
{
var displayObject: DisplayObject = sprite.getChildByName( name.toString() );
return ( displayObject is AS2SpriteHandle ) ? ( displayObject as AS2SpriteHandle ).handler : displayObject;
}
}[/as]

This may look a little bit fancy for the first time but what do we do? We check if the Sprite object already has that property. If so we return it. Otherwise we return the child of that sprite. Now you see that I check if the DisplayObject is a AS2SpriteHandle. That is the instance the AS2Sprite (our Proxy) stores. Because it keeps a reference back to its AS2Sprite object we can return the AS2Sprite object. If we return only Sprite we could do a.b but not a.b.c because for b we would return just Sprite and lose therefore the ability to add c just as a property.

This is important. The class AS2Sprite extends Proxy and not Sprite. Therefore I added some functions like toSprite() etc. Have a look. Examples help me often the most. And if you are a lazy guy that does not really want to bother with the AS3 syntax you might find this helpful as well.

Some example code what you can do with it:
[as]var sprite: AS2Sprite = new AS2Sprite;
addChild( sprite.toSprite() );//doh! default app is still sprite :(

sprite.clip = new AS2Sprite;

sprite.graphics.beginFill( 0xff0000, 1 );
sprite.graphics.drawCircle( 0, 0, 10 );
sprite.graphics.endFill();
sprite._x = 10;
sprite._y = 10;

sprite.clip.graphics.beginFill( 0×00ff00, 1 );
sprite.clip.graphics.drawCircle( 0, 0, 10 );
sprite.clip.graphics.endFill();
sprite.clip._x = 50;
sprite.clip._y = 50;

sprite.clip.clip2 = new AS2Sprite;
sprite.clip.clip2.graphics.beginFill( 0×0000ff, 1 );
sprite.clip.clip2.graphics.drawCircle( 0, 0, 10 );
sprite.clip.clip2.graphics.endFill();
sprite.clip.clip2._x = 50;
sprite.clip.clip2._y = 50;

delete sprite.clip.clip2;//remove the blue one[/as]

Related Posts

0 Responses to “AS3 Proxy Example”


  1. No Comments

Leave a Reply






Close
E-mail It