If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Following on from my previous post about memory leaks I thought I would show you the code I use for spotting for memory leaks at runtime.
See below
import flash.events.*;
import flash.system.System;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;
var tf:TextField = new TextField();
tf.border = true;
tf.textColor = 0x333333
tf.background = true
tf.autoSize = TextFieldAutoSize.LEFT;
addChild(tf)
initTimer();
function initTimer():void {
var myTimer:Timer = new Timer(1000, 0);
myTimer.addEventListener("timer", timerHandler1);
myTimer.start();
}
function timerHandler1(event:TimerEvent):void {
tf.text = String(Math.round(flash.system.System.totalMemory /1000 /1000)) + " MB \n " + String(flash.system.System.totalMemory) + " Bytes";
}
Found this useful? How about buying me a coffee - simply click here.
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.
Found this useful? How about buying me a coffee - simply click here.
Regular expressions are something that were introduced in AS3 with a sigh of relief from all flash developers. Yay
Below is a code snippet posted by senocular at kriupa
function isValidEmail(email:String):Boolean { var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(email);
}
//...
trace(isValidEmail("senocular@example.com")); // true
trace(isValidEmail("@example.com")); // false
trace(isValidEmail("senocular@example")); // false
trace(isValidEmail("seno\\cular@example.com")); // false
Its a beautiful thing.
Found this useful? How about buying me a coffee - simply click here.
The SimpleButton class is a sweet way to put sprites together to make a button. The SimpleButton is a light weight alternative to the heavier MovieClip object.
var myButton:SimpleButton = new SimpleButton();
myButton.upState = mySprite1;
myButton.overState = mySprite2;
myButton.downState = mySprite3;
myButton.hitAreaState = mySprite4;
You can also draw your sprites on the fly.
var myButton:SimpleButton = new SimpleButton();
//create the look of the states
var down:Sprite = new Sprite();
down.graphics.lineStyle(1, 0x000000);
down.graphics.beginFill(0xFFCC00);
down.graphics.drawRect(10, 10, 100, 30);
var up:Sprite = new Sprite();
up.graphics.lineStyle(1, 0x000000);
up.graphics.beginFill(0x0099FF);
up.graphics.drawRect(10, 10, 100, 30);
var over:Sprite = new Sprite();
over.graphics.lineStyle(1, 0x000000);
over.graphics.beginFill(0x9966FF);
over.graphics.drawRect(10, 10, 100, 30);
// assign the sprites
myButton.upState = up;
myButton.overState = over;
myButton.downState = down;
myButton.hitTestState = up;
Found this useful? How about buying me a coffee - simply click here.
I previous of flash there was no way to detect when the users mouse had left the flash movie.
Actionscript 3 now allows you to detect when the users mouse has left the movie using the mouseLeave event.
Lets take a look:
package {
import flash.events.Event;
import flash.events.MouseEvent;
public class mouseLeaveTest extends Sprite {
public function mouseLeaveTest() {
stage.addEventListener(Event.MOUSE_LEAVE, mouseHasLeft);
}
public function mouseHasLeft)(e:Event):void {
trace('Mouse have left the stage');
}
}
}
Found this useful? How about buying me a coffee - simply click here.
One of the pimping new features of AS3 is the ability to label your loops. At first you may think this is pointless but once you start using them you will find out how wonderful they truly are.
They come into their own when you have nested loop structure and you want to exit loops when a certain condition is met.
Kudos goes to senocular for me finding out about this cool feature.
In AS2 you would do the following to break a loop.
var i:Number;
var j:Number;
var exit:Boolean = false;
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (i > 3 && j > 3) {
exit = true;
break;
}
}
if (exit) {
break;
}
}
But with AS3 and labels you can break from a specific loop.
E.G
var i:Number;
var j:Number;
mainLoop: for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (i > 3 && j > 3) {
break mainLoop;
}
}
}
With labels the nested loop is able to break the mainloop. This makes the code cleaner and easier to read.
Nice.
Found this useful? How about buying me a coffee - simply click here.
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"));
Found this useful? How about buying me a coffee - simply click here.
Here is how you find all the movieclips inside an object in as3
var desc:XML = describeType(my_mc);
for each (var node:XML in desc.variable) {
if (node.@type == "flash.display::MovieClip") {
if (my_mc.getChildByName(node.@name.toString())) {
trace("child mc found");
}
}
}
Found this useful? How about buying me a coffee - simply click here.
This example shows you how to load and external asset (file) in AS3. The files can be a swf, jpg, gif or png.
This particular example loads a jpg called loademe.jpg and attaches to the stage
Load Asset / external files example
// loadAsset.as
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
public class loadAsset extends Sprite{
private var _loadPath:String
private var _loader:Loader;
// Constructor
public function loadAsset(){
// Create the loader
_loader = new Loader();
// Add event listener that tells us when the
// asset can be safely accessed
_loader.contentLoaderInfo.addEventListener(Event.INIT, displayAsset);
// Create new URLRequest
var urlRequest = new URLRequest("loadme.jpg")
// Start the asset load
_loader.load(urlRequest);
}
// Display the asset once the load is complete
private function displayAsset(ev:Event){
// it to the DisplayObjectContainer
addChild(_loader.content);
}
}
}
To load the asset/file you need to open the flash file and place this AS in the timeline
import loadAsset;
addChild(new loadAsset());
Found this useful? How about buying me a coffee - simply click here.
In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a click event to a object.
Full code after the jump.
Mouse Event - click
// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)
cir_mc.addEventListener(MouseEvent.CLICK, clickListner);
function clickListner(ev:MouseEvent):void{
trace("Circle Clicked");
}
Found this useful? How about buying me a coffee - simply click here.