If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
This is a simple function that returns a random value within a range.
function randomNumber(rangeMax:int = 1, rangeMin:int = 0):int { return Math.round(Math.random() * (rangeMax - rangeMin) + rangeMin); } trace(randomNumber(13, -3)); // Returns a random number between -3 and 13. trace(randomNumber(50)); // Returns a random number between 0 and 50. trace(randomNumber()); // Returns 0 or 1 as random number.
So as we all know in AS3 there is not such thing as _root or globals.
But sometimes you just really want and need global vars, so what to do.
Well never fear Paulius Uza has created a global class.
Global Object is a Singleton that lets you store dynamic variables in a globally accessible location within your AS3 application. This enables developers to accomplish things like self registering visual components, global events and event listeners.
I have used this class in many projects and found it great. Using it is simple see the example below
package { import flash.display.*; import lt.uza.utils.*; public class Test extends Sprite { private var global:Global = Global.getInstance(); private var testSprite:Sprite = new Sprite(); public function Test(){ //setting variables is easy, global object accepts any name / value pair, even functions global.stage = this.stage; global.testA = "a" global.testB = testSprite; global.testF = test; //getting variables is easy too trace(global.testA); this.addChild(testB); //as easy as calling a globally stored function global.testF(); } private function test():void { trace("this is a global test"); } } }
Click here to find out more and download the class
Now one of the biggest features of as3 is the management of memory within flash and flex.
In my experience event listeners are the source of about 95 percent of all memory leaks. This is because event listeners are often forgotten by developers, which normally results in the listener never being removed from memory.
This is where weakly referenced event listeners come into their own.
Weakly references to objects that are not counted by the Garbage Collector in determining an object’s availability for collection. So if you forget to remove the listener you wont stop the Garbage Collector’s ability to collect the object and freeing your memory. Yay…
So from that stand point alone I suggest that you use Weakly reference Event listeners as standard practice.
To make your event listener weakly then just set the fith param in the addEventListner to true. See code below
stage.addEventListener(Event.CLICK, handleClick, false, 0, true);
Nod to the wondeful gSkinner for this.
Returns the number of words in a string.
function wordcount(string:String):Number {
var wrdArray:Array = string.split(" ");
for (var i = wrdArray.length; i>0; i--) {
if (wrdArray[i] == "") {
wrdArray.splice(i,1);
}
}
return wrdArray.length;
}
trace(wordcount("This will count the number of words"));I had this problem and thought you would all like to know….. I was trying to load a local xml doc and this solution worked for me.
Posted on January 11th, 2007 by polyGeek
Error (#2148) Loading XML with Flex2
[RPC Fault faultString=”Error #2148: SWF file file:///C:/Documents and Settings/…/My Documents/xmlParsing/bin/xmlParsing.swf cannot access local resource Blades.xml. Only local-with-filesystem and trusted local SWF files may access local resources.” faultCode=”InvokeFailed” faultDetail=”null”]
If I go into the bin folder and launch the SWF directly then everything loads and works correctly. That right off is a tip that it’s probably a security issue. Sure enough. With a little research I found it.
So my arguments read: -locale en_US -use-network=false
It would seem that this setting has to be made for each project.
Stupid security settings.
When I Googled this problem I only got three hits back. My guess is that this will be a common problem so it wouldn’t hurt for others to duplicate this solution to make it easier for others to find.
Here the first in a series of installments that will shed some light on Enterprise Flex RIA’s
“In this installment of the series Anatomy of an Enterprise Flex RIA we’re doing a little grunt work and installing some tools we’ll be using later. Download them and read up a bit on what they do. These tools are some common “enterprise software” development tools that we’ll need to get the example software up and running.
In this series, we’ll look at a small application that integrates the technologies of LCDS and EJB 3.0, and we’ll cover some timesaving tools as well as discuss how to use them to achieve a lightweight development environment for integrating an RIA with an enterprise environment. Ready to get started?”
http://www.insideria.com/2008/01/anatomy-of-an-enterprise-flex.html