Wednesday, March 16, 2011

3D actionscript objects to bitmaps example

Here's a quick post on how to convert your 3d spark component into a bitmap for use with the drag manager. I'm not going to wax poetic about this , if anyone want's further explanation please let me know and I'd be happy to go into details.

The dice object here is a 3D skinnable component. In order to keep the perspective
the same when converting it to bitmapData, you need to have your perspectiveProjection properly set.

var ds:DragSource = new DragSource();
dice.transform.perspectiveProjection = new PerspectiveProjection();
dice.transform.perspectiveProjection.projectionCenter = new Point(400,220);
dice.transform.perspectiveProjection.fieldOfView = 10
var bitmapData:BitmapData = BitmapUtil.getSnapshot(dice);
var bitmap:Bitmap = new Bitmap(bitmapData);
var snap:Image = new Image();
snap.width = 80;
snap.height = 80;
snap.load(bitmap);
DragManager.doDrag(dice,ds,event,snap,0,00,1.0);

I ended up not even using the drag manager in the application this is from, but the research it took to get this far was too good to forget.

Friday, March 4, 2011

Switch states in Flash application example

Here's a quick problem I had to deal with last night. If you are making a programmatic Flex application and you need to switch the application state (the states you can tie various views to in your mxml code) you will have to first get a reference to the application object.

import mx.core.FlexGlobals;
import spark.components.Application;

public class StandardClass
{

private var _application:Application = mx.core.FlexGlobals.topLevelApplication as Application;

}

Note the imports above, there are many different application objects to choose from, but if you are making a standard Flex application you will want to use the Spark object. Also, you cannot apparently reference static things in FlexGlobals without first importing it. This is different behavior then you would see in Java.

So next we just need to simply pass the application object the name of the state we wish to invoke.
javascript:void(0)

application.currentState = "State2";


And that's it! If you're interested in setting transition animations you can check out the "transistions" property of the application object to set custom animations.