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.
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.
In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a mouse up event to a object.
Mouse Event - up
// 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.MOUSE_UP, clickListner);
function clickListner(ev:MouseEvent):void{
trace(”Mouse Up”);
}
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 mouse over event to a object.
Mouse Event - over
// 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.MOUSE_OVER, clickListner);
function clickListner(ev:MouseEvent):void{
trace("Mouse Over Circle");
}
Found this useful? How about buying me a coffee - simply click here.
It’s so massive that its currently only available as a series of PDFs. It’s the article everyone has been waiting for from Adobe:
Create ActionScript 3.0 components in Flash CS3 Professional
Found this useful? How about buying me a coffee - simply click here.