Monday, March 4, 2013

Flash Builder: Building Crashes Player - Vector Issue

While working with some vectors in AS3, I was in a hurry and typed out my vectors like this:

private var image_types:Vector.<String> = new Vector.<String>("png",'jpg','jpeg','gif');

This caused the Flash Player to crash without giving an error and needless to say, I had to reverse my steps until I figured out it was this Vector that was the problem.

In the code I displayed above, I assumed that I could define some elements on Vector construction like you can with the Array class. This assumption is what caused the problem and ultimately a bug that isn't isn't caught when compiling the code down, but unexpectedly caught on execution. The Vector class doesn't allow you to pass list items into the constructor, but instead allows you to pass an uint and a Boolean which tell it what length to have and if it should be fixed.

The solution was simple enough though, using the Vector global function, I was able to modify my code slightly and have it compile and execute as expected.

private var image_types:Vector.<String> = new Vector.<String>(["png",'jpg','jpeg','gif']);

Friday, March 1, 2013

AS3 : Setting the Mouse Cursor

Sometimes it can be difficult to use custom cursors in AS3.

The Mouse singleton class makes this really easy and fluid. If you compare a side-by-side of a SWF using the old techniques vs the Mouse singleton class, you will notice the difference.

Example: Setting the cursor to an image.

// Embed a graphic for the custom cursor
private var DrawCursor:Class;

// Setup the cursorData object
private var cursorData:MouseCursorData = new MouseCursorData();

// First we setup a vector to handle the bitmapdata objects (you would use more than 1 if you wanted an animated cursor).
var drawCursorData:Vector.<BitmapData> = new Vector.<BitmapData>();
// Now we need to get the bitmap from our embedded graphic
var drawCursor:Bitmap = new DrawCursor();
// now populate the bitmapdata vector with our graphic bitmap data
drawCursorData[0] = drawCursor.bitmapData;
// the default line-up with your cursor's pointer is 0,0 - but if you need to adjust it, you can set the hotSpot to a new x,y
cursorData.hotSpot = new Point(2,12)
// set the cursordata to contain the bitmapdata vector = drawCursorData;
// register the cursor so you can use it later
Mouse.registerCursor("drawCursor", cursorData);

// When you're ready, set the mouse cursor using the cursor property to the registered name.
Mouse.cursor = "drawCursor";

// And when you're all done, you can restore the cursor back using the static property of the MouseCursor class
Mouse.cursor = MouseCursor.AUTO;

Note: There are several default cursors available in the MouseCursor class like Arrow, Button, Hand, and IBeam.

Good Luck!