Code.Flash MX: Solution for using relative paths in swfs to load data.
Only starting from Flash 9 and Actionscript 3 we can reliably use relative path to load data. Let me explain the problem.
say for example we have an swf file named 'Slide.swf' in a folder 'Slide001' folder which loads 'data.xml' using the following statement
my_xml=newXML();
my_xml.onLoad=function(success){
//process the xml here
}
my_xml.load('data.xml')
This works fine when we directly call the swf, or the html page that embeds the swf stays in the same folder.
But if you load the swf('Slide.swf') inside another swf which resides in the parent folder, 'data.xml' is expected at the parent folder instead of 'Slide001'
Same happens when we move the html to parent folder. The prototype I've given below, which finds the path of the current swf can solve this problem.
MovieClip.prototype.addProperty('path', function () {
if (this._path == undefined) {
var a = this._url.split('/');
a.pop();
this._path = a.join('/')+'/';
}
returnthis._path;
}, null);
Once you have the above prototype change the xml.load statement as shown below
my_xml=newXML();
my_xml.onLoad=function(success){
//process the xml here
}
my_xml.load(this.path+'data.xml')
XML Shortcuts helps to access the XMLNodes with ease. You may read my previous post which compares the XMLShortcuts with XPath from XFactor Studio, XML to Object Prototype, and XMLNode easy access by tatsuo kato
Here is a example file, that shows how you can use XMLShortcuts in your MTASC projects.
class XMLShortcutsExample
{
function XMLShortcutsExample ()
{
//Activate the XML Shortcuts
XMLShortcuts.activate ();
//Create an XML Object
//Note: Avoid declaring as XML
var my_xml = newXML ()
//ignoreWhite=true is not a must for XML Shortcuts, but it can improve performance
//for the sake of display we ignore it here
//_xml.ignoreWhite=true;
my_xml.onLoad = function (success)
{
if (success)
{
_root.t1_txt.text += "Original XML:\n" + this;
_root.t2_txt.text += "my_xml.english.a = " + my_xml.english.a + "\r\n";
_root.t2_txt.text += "my_xml.english.a_1 = " + my_xml.english.a_1 + "\r\n";
_root.t2_txt.text += "my_xml.english.$b = " + my_xml.english.$b + "\r\n";
_root.t2_txt.text += "my_xml.english.c._word = " + my_xml.english.c._word + "\r\n";
_root.t2_txt.text += "my_xml.english.c.__text = " + my_xml.english.c.__text + "\r\n";
_root.t2_txt.text += "my_xml.english.a_1.nodeIndex = " + my_xml.english.a_1.nodeIndex + "\r\n";
_root.t2_txt.text += "my_xml.english.c.rootNode instanceof XML = " + (my_xml.english.c.rootNode instanceofXML) + "\r\n";
}
}
//Load the XML
my_xml.load ("XMLShortcutsExample.xml");
var tf : TextFormat = newTextFormat ();
tf.font = "Courier";
//Create a TextFied to display
_root.createTextField ("t1_txt", 0, 0, 0, 300, 400);
_root.t1_txt.text = "XMLShortcutsExample:\n\n";
_root.t1_txt.setNewTextFormat (tf);
_root.createTextField ("t2_txt", 1, 300, 0, 500, 400);
_root.t2_txt.text = "XMLShortcuts Usage:\n\n";
_root.t2_txt.setNewTextFormat (tf);
_root.t2_txt.background = true;
_root.t2_txt.backgroundColor = 0xDDDDDD;
}
staticfunction main () : Void
{
var app : XMLShortcutsExample = new XMLShortcutsExample ();
}
}
above script produces the swf below, when compiled using the following command
Extension.Flash MX: XML Shortcuts V2.2 MTASC Edition released.
Now XMLShortcuts meets MTASC :)
I received many requests to make XMLShortcuts available for MTASC (Motion Twin Action Script Compiler). I could not do it with my busy schedule so far, but finally here it is
Download package consists of XMLShortcuts.as, an example and help files
Code.Flash MX: String.isWhite and String.getWordCount().
This is the same word count function that I showed in the previous post presented in the good old way of adding more properties and methods through the prototype.
String object is extened to have isWhite read only property and getWordCount method.
String.prototype.addProperty('isWhite', function () {
return !isNaN(this+' 0');
}, null);
/*Usage Example:-
var str = " \t \r";
trace(str.isWhite);
//traces true
str+=".";
trace(str.isWhite);
//traces false
*/String.prototype.getWordCount = function() {
if (this.isWhite) {
return 0;
}
var arr = this.split(' ');
var l = arr.length;
for (i in arr) {
if (arr[i].isWhite) {
l--;
}
}
return l;
};
/*Usage Example:-
var str = "World is \r Not Enough. ";
trace(str.getWordCount());
//traces 4
*/
Code.Flash MX: Counting number of words in a String.
Here is a simple code to get the word count. To ease up the process, I do not count the words that are not separated by space
For Example "Mr.Arul" will still be counted as one word. but it makes sure whitespace is not counted as words
Here is the code
function wordcount(txt) {
if (!isNaN(txt+" 0")) {
return 0;
}
var arr = txt.split(' ');
var l = arr.length;
for (var i in arr) {
if (!isNaN(arr[i]+" 0")) {
l--;
}
}
return l;
}
/*Usage Example:-
trace(wordcount("I Love Flash "));
//traces 3
*/
Examples.Flash Lite: Generating Unique Random Number for Flash Lite 1 and 1.1.
Generating Unique random number for Flash Lite 1.1 is tricky because of the Flash 4 style syntax. I've made a sample application which shows how this can be achieved.
Here is the sample code to shuffle the contents of an array
// Here is a simple function for randomizing the array
function randomsort(a, b) {
returnMath.random()>.5 ? -1 : 1;
}
//usage example
var arr = [1, 2, 3, 4, 5];
arr.sort(randomsort);
trace(arr);
//traces 4,3,1,5,2
Tips.Flash MX: including Actionscript 1 files in Actionscript 2 Project.
When we are including an AS1 file in an Fla which has to be published as AS2, we need to be careful in naming the include file. There shouldn't be any object initialized in _global which has the same name as the include file.
Say for example,
I had a include file called Security.As and inside that had _global.Security=new Object(). When I published I got the following error
**Error** C:\Documents and Settings\areal\Desktop\Security.As: Line 1: ActionScript 2.0 class scripts may only define class or interface constructs.
Security=new Object();
Total ActionScript Errors: 1 Reported Errors: 1
To resolve this issue we need to rename the Security.as file to something else, for example SecuritySystem.as and change the include statement accordingly
posted by Arul
| link
|<prev. | ^top
| next> |
comments [1]
Saturday, June 03, 2006
Tips.Flash MX: Is Running in Flash IDE?
When testing my application in IDE, I wanted to use some hard coded test data instead of Every time loading the dynamic data. System.capabilities.isDebugger is the boolean property which is true when we are testing in IDE or special Debug Player.
Here is the sample code that I've used
if (!System.capabilities.isDebugger) {
_myxml.load(xmlPath+"?"+"policynumber="+policynumber+"&PolyType="+PolyType+"&strSelectAgentCode="+strSelectAgentCode+"&strCountryCode="+strCountryCode+"&policyStatus="+policyStatus+"&AgncyCde="+AgncyCde+"&passinagency="+passinagency);
} else {
_myxml.load("xml/table.xml");
}