What is an alternative to flash config constants? - actionscript-3

We have a project that has been built in Flash and as3. It is a video player of sorts that we want to fully customize. We have different images and color schemes that we want to be able to change very quickly. Right now we have config constants that we turn on and off for different schemes. And in the code there is a massive amount of different spots where the images and such are changed.
When we create a new color scheme or whatever, we need to create a new config. Then we have to go through all of the code and put it in correctly.
Basically any suggestions for how we can take the current flash project (maybe flex?) and make it customizable a lot quicker.

Move all configurable parameters to an XML definition.
Create multiple XML documents for each customization.
In code, establish default values for configurable parameters, then load the XML and reference values of the XML document as overrides to those defaults.
For a production build, XML can be embedded in the assembly if loading an external resource is an issue for deployment.
By loading different configuration XML documents, you could change the definition during runtime, and by using the dynamic configuration model you could draft a theme editor to view changes real time.
ConfigurationModel.as
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class ConfigurationModel
{
/** ======== configuration ======== */
public static var color:uint = 0xff00ff;
public static var fontName:String = "Arial";
/** ======== serialization ======== */
public static function loadConfiguration(url:String):void
{
var loader:URLLoader = new URLLoader(new URLRequest(url));
loader.addEventListener(Event.COMPLETE, completeHandler);
}
protected static function completeHandler(event:Event):void
{
var xml:XML = new XML(event.target.data);
if (xml.color)
color = xml.color;
if (xml.fontName)
fontName = xml.fontName;
}
}
}
Example configuration: AcmeClientConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<color>0xff0000</color>
<fontName>Calibri</fontName>
</configuration>

Related

AS3 URLRequest Local File on multiple OS/Browsers

Greatings!
I have yet another question concerning the loading of a .swf inside a existing one.
In my game. I have a introduction screen in which I load another .swf (which is a movie).
This all works fine and the URLRequest is this:
request = new URLRequest("Movie.swf");
As I said, this works fine. However when I copy my game.swf and movie.swf to a USB stick.
(I put them in the same directory to prevent other issues).
It doesn't seem to find the movie.swf.
Now I know that it has to do with the path given in the URLRequest and/or the publish settings. But I do not know how to make this so that it searches in the same directory
as the game.swf is in.
I hope you guys have an answer for this issue.
Thanks in advance,
Matti.
Matti, I believe Lukasz's comment is correct about it being a security error.
You can avoid this security error by embedding Movie.swf instead of using a Loader. If you do this, then at compile-time the Movie.swf needs to sit next to the Game.as file, and it will be included in the Game.swf (no need to deliver both files, just Game.swf).
The syntax is:
package
{
import flash.display.Sprite;
public class Game extends Sprite
{
[Embed(source="MyMovie.swf")]
private var myMovieClass:Class;
private var myMovie:DisplayObject;
public function Game():void
{
myMovie = new myMovieClass();
// Technically myMovie is now a Loader, and once
// it's loaded, it'll have .content that's a
// MovieClipLoaderAsset, and .content.getChildAt(0)
// will be your MyMovie.swf main timeline.
}
}
}
Alternately, if you embed it as mimeType="application/octet-stream", you can get the bytes of the SWF and use it in your existing Loader's .loadBytes() method:
package
{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class Game extends Sprite
{
[Embed(source="MyMovie.swf", mimeType="application/octet-stream")]
private var movieBytes:Class;
private var myMovie:DisplayObject;
public function Game():void
{
// Treat this loader the same as your current loader,
// but don't call .load(url), call loadbytes():
var l:Loader = new Loader();
l.loadBytes(new movieBytes() as ByteArray);
}
}
}

embedding a font in a swf using as3

I have a project using flash (and AIR for android and eventuall iOS) for translating some phrases. Although most fonts are fine there are some that I have to load after the user has chosen the languages. Over a year ago I generated a couple of swf files (Bengali and Urdu) and put them on my web server. The flash application then loads them when required and everything is OK...
However as I am nearing implementing the project I thought I should generate the swf files for the other languages that are in the pipeline and for some reason I can't manage it! Needless to say I have misplaced (tidied) the original AS3 source for the two fonts I have done.
package
{
import flash.display.Sprite;
import flash.events.Event;
public class ArabicF extends Sprite
{
[Embed(source = "trado.ttf",
fontName = "ArabicX",
fontFamily = "ArabicY",
fontWeight = "normal",
fontStyle = "normal",
mimeType = "application/x-font",
advancedAntiAliasing="true",
embedAsCFF="true")]
public static const ArabicZ:Class;
}
}
This has expanded from a much simpler form as I added things to get it to work and I must have tried all permutations of true and false. I load the swf ok but then can't extract the class. With the two fonts I embedded before the swf class and the parameter all have the same name and they work fine (i.e.
Urdu.swf
then later:
var FontClass:Class = evt.target.applicationDomain.getDefinition("Urdu") as Class;
and then
Font.registerFont(FontClass.Urdu);
in the calling .as application with the above Arabic.swf I try
trace("1 Arabic "+evt.target.applicationDomain.hasDefinition("ArabicF"));
trace("2 Arabic "+evt.target.applicationDomain.hasDefinition("Arabic"));
trace("3 ArabicX "+evt.target.applicationDomain.hasDefinition("ArabicX"));
trace("4 ArabicY "+evt.target.applicationDomain.hasDefinition("ArabicY"));
trace("5 ArabicZ "+evt.target.applicationDomain.hasDefinition("ArabicZ"));
but all return false
PS also tried generating the Arabic.swf using fontswf.bat which again seems to make a very similar swf file which is loaded but I can't extract the class from it
PPS I'm using flashdevelop and the font swf are set up as AS3 standard project compiler options.
I don't use Flash Develop, but perhaps I can offer some perspective from my implementation.
If I wanted to embed Arial, the embedding swf would "Export for ActionScript" the embedded font. In its document class, use registerFont() as below:
Font.registerFont(Arial);
Like every other swf, you'd use a Loader to import it into your runtime, and at that point, the font will be available to your global table. You can query the complete list of registered fonts using enumerateFonts().
var fontList:Array = Font.enumerateFonts();
for (var i:int = 0; i < fontList.length; i++) {
trace(fontList[i].fontName);
}
Assuming we've got a TextField called "txt", you can then implement that font with setTextFormat() like so:
var format:TextFormat = new TextFormat();
format.font = "Arial";
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
txt.setTextFormat(format);
txt.defaultTextFormat = format;
A disclaimer from Adobe on the use of these last two settings:
"When you apply a TextFormat object to a text field using the
TextField.defaultTextFormat property or the TextField.setTextFormat()
method, only its defined properties are applied. Use the
TextField.defaultTextFormat property to apply formatting BEFORE you
add text to the TextField, and the setTextFormat() method to add
formatting AFTER you add text to the TextField."
As I said before, my workflow doesn't utilize Flash Develop, but rather Flash IDE to embed the fonts into the swfs. Your results may vary, but be aware you must set embedFonts to true, or it won't respond to your embedded font. I've lost hair on that issue before.
Hope that helps. Cheers,
OK In case someone else runs into similar problems this is stage one of the answer (really the answer to my original question) still have to sort out how to get the font to be used in the flash.font.engine apparatus!
The critical parts are:
Point #1. the embedded font swf must have a name that doesn't clash with ANY name registered in the calling script. Bizzare and (as far as I could search) undocumented. i.e. I have a list of languages uploaded via xml services to an xml object and that includes for instance 'English' however the font loading doesn't work until I make the loadable font file into 'EnglishF.swf' The obvious choice of just 'English.swf' fails. So my embedded font source is now called EnglishF.swf and reads:
package
{
import flash.display.Sprite;
import flash.text.Font;
import flash.system.Security;
Security.allowDomain("*");
/**
* ...
* #author patrick
*/
public class EnglishF extends Sprite
{
[Embed(source = "Kingthings Exeter.ttf",
fontName = "EnglishF",
fontFamily = "EnglishF",
fontWeight = "normal",
fontStyle = "normal",
mimeType = "application/x-font",
advancedAntiAliasing="true",
embedAsCFF="true")]
public static var myFont:Class;
}
}
and the using script goes:
private function fontLoaded(evt:Event):void {
var FontClass:Class
if (evt.target.applicationDomain.hasDefinition(font1Name)) FontClass = evt.target.applicationDomain.getDefinition(font1Name) as Class;
else if (evt.target.applicationDomain.hasDefinition(font2Name)) FontClass = evt.target.applicationDomain.getDefinition(font2Name) as Class;
try {
Font.registerFont(FontClass.myFont);
trace("successfully loaded " + FontClass);
} catch (err:Error) {
trace("couldn't register font "+FontClass.myFont+" =>"+err);
}
}
The line in the embedded script about security is because of point #2: the registerFont doesn't work across domains. In fact the Security.allowDomain() didn't work and to debug this I have had to copy/paste the swf files on the PC and change the loader.load(URLRequest()) accordingly
There is still an issue that although the font is registered and claims to be compatible with the flash.text.engine TextElement requirements it still fails to be used as in:
_tl = new Array();
var block:TextBlock = new TextBlock();
var font:FontDescription = new FontDescription(fontName);
var formt:ElementFormat = new ElementFormat(font, fontSize);
//following gives 'true EnglishF'
trace(FontDescription.isFontCompatible(fontName,"normal","normal"), formt.fontDescription.fontName);
formt.color = colr;
var span:TextElement = new TextElement(text, formt);
block.content = span;
_tl[0] = null;
_tl[0] = block.createTextLine(null, width);
var tl:TextLine;
// after here justification and addition lines
I will post something if and when I sort out why this doesn't work (Unless some kind person does it for me!)

How to access variables of the 'main' SWF from a loaded SWF by SWFLoader?

I'm working on a website built in Flex, but the SWF file after compiling is very large. The websites has multiple pages.
So I thought to create a new MXML project for every new page and load it with SWFLoader.
I've found this example:
public function extSwfLoaded(evt:Event):void {
var sysmgr:SystemManager = (extSwf.content as SystemManager);
sysmgr.addEventListener(FlexEvent.APPLICATION_COMPLETE, function(event:FlexEvent):void {
var sysmgr:SystemManager = (event.currentTarget as SystemManager);
var swfApp:Application = (sysmgr.application as Application);
}
});
public function gotoPage(page:String):void {
extSwf.addEventListener(Event.INIT, extSwfLoaded);
var now:Date = new Date();
switch(page) {
case "register":
openedPage = "register";
extSwf.load('modules/register.swf?anticache=' + now.getTime());
break;
}
}
And in the MXML:
<mx:SWFLoader id="extSwf" complete="extSwfLoaded(event)" width="100%" />
<s:Label text="Register" useHandCursor="true" buttonMode="true" click="gotoPage('register')" />
This works perfect. The content of modules/register.swf is showed at the place of the extSwf SWFLoader.
But: I've no idea how to interact with the 'main' SWF and the loaded SWF file.
I have some global variables that I want to send to every loaded SWF file
(and some variables that I want to send from the loaded SWF file to the 'main' SWF file).
A website said that I can send these variables by loading modules/register.swf?var1=hi&var2=hello, but someone who can see the HTTP headers (for example with Live HTTP Headers in Firefox) can see all these variables.
So, is it possible to load a SWF file and send them some variables? And when I have a new global variable, I don't have to open and edit and recompile every MXML project?
Thank you very much!
It looks like this can show the variables of my 'main' MXML.
import mx.controls.Alert;
import mx.core.FlexGlobals;
Alert.show(FlexGlobals.topLevelApplication.myvar);
And for using public functions:
FlexGlobals.topLevelApplication.myFunction();
I just needed the right keywords (top level application) to search for it on the internet.

Reducing the number of Class objects to access embedded resources

I'm building a Flex project with a lot of embedded bitmaps and such. The usual method for getting to the bitmaps in Actionscript seems to be to do something like
[Bindable] [Embed(source = '../lib/WhiteFencePost.png')]
private static var clsObstacleFencePost : Class;
var bitmap : BitmapAsset = new clsObstacleFencePost();
I've already got several dozen of these things, and I can easily see ending up with hundreds of them by the time the project's done. Is there some way that I can avoid creating a Class for every bitmap?
Try to create an assets manager with static classes like this.
class AssetManager
{
[Bindable]
[Embed(source = '../lib/WhiteFencePost.png')]
public static var WhiteFencePost:Class;
[Bindable]
[Embed(source = '../lib/BlackFencePost.png')]
public static var BlackFencePost:Class;
}
Then you can use the images like this
myImage1.source = AssetManager.WhiteFencePost;
myImage2.source = AssetManager.WhiteFencePost;
myImage3.source = AssetManager.WhiteFencePost;
myImage4.source = AssetManager.BlackFencePost;
You do not need to define a new instance of the Class images that you want to use.
There are ways... Pack all files into zip - embed zip - unpack with any zip library for actionscript, then Loader.loadBytes pic needed. Zip contents can be enumerated, so if you know what to do with files by name, you don't even need file list in application.

Saving XML file in AS3 is possible

var xml:XML = <myXml>
<item prop="1" />
<item prop="2" />
</myXml>;
I need to save as xml file in local harddisk(project directory).
Is it possible to save in as3 itself?
I threw this together, and sure enough you can save to .XML using the following as a minimalist example.
package com.hodgedev.xmlcreator
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.net.FileReference;
/**
* ...
* #author Brian Hodge (brian#hodgedev.com)
*/
public class Main extends Sprite
{
private var _xml:XML;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Calling the save method requires user interaction and Flash Player 10
stage.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
_xml= <xml>
<test>data</test>
</xml>;
}
private function _onMouseDown(e:MouseEvent):void
{
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(_xml);
//ba.
var fr:FileReference = new FileReference();
fr.addEventListener(Event.SELECT, _onRefSelect);
fr.addEventListener(Event.CANCEL, _onRefCancel);
fr.save(ba, "filename.xml");
}
private function _onRefSelect(e:Event):void
{
trace('select');
}
private function _onRefCancel(e:Event):void
{
trace('cancel');
}
}
}
There are some things to note.
You require Flash Player 10 to use the save method of the FileReference class.
In order to do anything that INVOKES a prompt, Flash requires user interaction like keyboard or mouse input.
In the above I listen for MouseEvent.MOUSE_DOWN on the stage to serve as the USER INTERACTION which is required to invoke the save prompt.
I setup a basic XML structure within the code (this will typically come from and external source and will work fine both ways.
A ByteArray is created and the XML is written to the ByteArray.
The save method of the FileReference class requires a ByteArray and default save name be passed as the two parameters.
I hope this helps.
if you want to store it locally (on the client PC) , you can use a local shared object. Refer to this tutorial
I'm sorry, your question isn't very clear.
Are you asking if you can save a file to the hard drive from within a compile SWF written in AS3?
Or are you asking if you can include a raw XML file in your AS3 project without needing to write it out as a variable?
If you meant the former, no -- not without Adobe AIR. You can save data locally as a SharedObject, but not as an arbitrary file in the file system.
If the latter, then yes -- you must embed the file just as you would embed another resource (such as an image or a sound). However, it looks like there might be a bug in Flash that makes this non-trivial to figure out how to do.
This link might be of help to you.
[Embed(source='../../../../assets/levels/test.xml', mimeType="application/octet-stream")]
public static const Level_Test:Class;
And then to parse the XML:
var ba:ByteArray = (new Levels.Level_Test()) as ByteArray;
var s:String = ba.readUTFBytes( ba.length );
xml = new XML( s );
Apologies if neither of those questions are what you were actually asking.
Cheers!