Author Archive for admin

11
Jul

Finding Memory Leaks - Memory Usage Guage

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.

09
Jul

Memory Managment - Memory Leak

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.

16
Apr

AS3 Email Validation Code Snippets - Regular Expression

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.

15
Apr

Flash Player Update Now Available (Flash Player 9,0,124,0)

Last week adobe released there latest flash player

Find it here.
http://www.adobe.com/go/getflashplayer

Found this useful? How about buying me a coffee - simply click here.

14
Apr

Simple Button Class - AS3

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.

13
Apr

Detect when the mouse leaves the movie.

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.

12
Apr

Tips for learning ActionScript 3.0

ActionScript 3.0 is a powerful object-oriented language that represents a new programming model for the Flash Player runtime. If you are already familiar with ActionScript 1.0 or 2.0, you should be aware of some language differences as you develop your first application using ActionScript 3.0.

To help the transition adobe has compiled tips and common issues you might encounter during development.

http://www.adobe.com/devnet/actionscript/articles/actionscript_tips.html

Found this useful? How about buying me a coffee - simply click here.

11
Apr

Simple Find and Replace Function

This is a simple but effective function allows you to search a string and replace characters

function searchAndReplace(holder:String, searchfor:String, replacement:String) {
		var temparray:Array = new Array();
		temparray = holder.split(searchfor);
		holder = temparray.join(replacement);
		return (holder);
	}

Found this useful? How about buying me a coffee - simply click here.

10
Apr

Label Statements

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.

16
Feb

Word Count

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.




Close
E-mail It