Archive for the ‘Examples’ Category

Loading external classes in precompiled swf

Sunday, February 20th, 2011

Last year -march april 2010- meanwhile I was developing www.pipoideas.com (site at which I work together with Jorge Peraza) because of the 3D enviroment and because some incompatibilities, some textfield did not work well with embed fonts; my fastest idea was to find code or library to load a .ttf or .otf and be able to apply that font to a TextField. It didn’t work.

I had to read a lot to find that the closest solution was to embed the font inside a swf and load that swf, then extract the class, and then apply that class to the TextField.

At the end of this research I created two small but effective classes: “ExternalClass” and “ExternalFont”.

Using ExternalClass is easier than ExternalFont, so I decided to split them by its behaviour, this because ExternalFont lets apply directly to the TextField the required font.

With ExternalClass if you already have an swf with a class inside it, you can load it, and then call it:


import net.absulit.arbolnegro.data.ExternalClass;

var _externalClass: ExternalClass;
_externalClass = new ExternalClass();
_externalClass.path = "MyPrecompiledClass.swf";
_externalClass.addEventListener(Event.COMPLETE, onCompleteExternalClass);

private function onCompleteExternalClass(e:Event):void
{

	var c:Class;
	c = _externalClass.getClass("MyPrecompiledClass");
	var myvar:* = new c();
}

At this point you can access all the methods and properties from MyPrecompiledClass, as if it were another class.

Using ExternalFont the way the class works changes a bit, because all we need it’s to apply it to a TextField as soon as it loads. Previously so we can be able to use the font, it must be precompiled inside a swf, all this in a brand new .fla file; you must go to the library (F11), right click and add New Font, after this and compile it, you shall be able to access it via ExternalFont using the class name you give it via the window dialog:


import net.absulit.arbolnegro.data.ExternalFont;

private var _externalFont:ExternalFont;

_externalFont = new ExternalFont();
_externalFont.addEventListener(Event.COMPLETE, onCompleteExternalFont);
_externalFont.path = 'Arial.swf';

private function onCompleteExternalFont(e:Event):void
{
	_externalFont.setFont('Arial', mytext_txt);
}

To avoid confusions I preffer to use the same font type name to name the .swf and the class to load; so when I need the name, I take a look to the .swf and that is the name I need

Loading images through as3 and Arbol Negro

Friday, July 2nd, 2010

One of the most repetitive processes when using flash is loading images.
At the time of loading an image, you have to call several class to load a single image, and repeat the process can be confusing, if there is no class that handles the concept of image.

Usually when loading an image with Loader, it is placed inside a Sprite, so by default, SimpleImage is a Sprite, but you can also access the loaded content in the form of DisplayObject.

The following example also shows an image loaded twice through SimpleImage, both have been rotated 45 and scaled to 2.5, the image on the right has enabled smooth property to avoid pixelation of the image.

_simpleImage = new SimpleImage();
addChild(_simpleImage);
_simpleImage.x = 500;
_simpleImage.rotation =  45;
_simpleImage.path = _ocean;
_simpleImage.scaleX = 2.5;
_simpleImage.scaleY = 2.5;
_simpleImage.smooth = true;

Same Image Loaded twice

fig1. Left image pixelated, right image softened

The idea of using smooth is because sometimes the size of the image is not correct, is of low quality, and so we reduce the errors.

The advantages of using SimpleImage will be reflected in time savings, moreover, to be used as a Sprite, which means you can manipulate in any manner permitted by this one, or convert to MovieClip, scale, filters, create masks.

SimpleImage also has the ability to read the progress of the data stream to find out the percentage loaded.

Download example files:

Example-SimpleImage.zip

Loading XML with ActionScript and Arbol Negro

Thursday, May 27th, 2010

One of the most common task in actionscript in loading XML, it is the native way to load complex data; but there is a time when this process become a little anoying, because as you remember, in as2, load XML was just a couple of lines; in as3 is not that hard, but there are a couple of separate steps to load it and it’s sometimes confusing for begginers.

I have created XMLHandler to carry on those repetitions to “the easy way”, here is an example:


package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import net.absulit.arbolnegro.data.XMLHandler;
/**
* ...
* @author Sebastián Sanabria Díaz
*/
public class Main extends MovieClip
{

private var _xmlHandler:XMLHandler
public function Main()
{
_xmlHandler = new XMLHandler();
_xmlHandler.addEventListener(Event.COMPLETE, onCompleteXMLHandler);
_xmlHandler.addEventListener(ProgressEvent.PROGRESS, onProgressEvent);
_xmlHandler.path = "data.xml";
}

private function onProgressEvent(e:ProgressEvent):void
{
trace(e.bytesLoaded / e.bytesTotal);
}

private function onCompleteXMLHandler(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, onCompleteXMLHandler);
//trace(e.target.xml);//using event parameter
trace(_xmlHandler.xml);//using the private var
}

}

}

This is a very simple example, loads XML, traces it, also shows its progress, which in a bigger XML file will be more useful.

Download example files:

Example-XMLHandler.zip

Switch to our mobile site