AS3: Quickhint: Shared Objects

Last week while working on the interface for my new upcoming library I needed a way to save some data on the users computer, altough I’m working with AIR I could easily save an xml or a txt file to the users local computer, but as soon as I would start writing a pure as3 project I would have to rewrite it again, as I can’t be sure that the client will have Flash Player 10 or even the rights to save to it’s local harddisk, some people working at bigger companies are not allowed to save anything to their local harddisk due to security rights.

The solution for this is simple yet effective, you need to use SharedObjects aka. Cookies in Flash.

Shared Objects can do the following:

  • Maintain Local Persistence: Saves data locally on the users drive, for example you could save the last application state or the score of the last game, create a local highscore list and many many more applications for local use.
  • Store and Share Data on FMS: Basically the same as before, but with the difference that you saving the shared object on a flash media server. Good thing about it, it can be shared throughout multiple computers, the same user using the same application on multiple computers can save its data on a server.
  • Share data in realtime: using FMS you can aswell for example save the state of a multiuser application saving the current state of the applcation, every new user will get the current state based on the shared object

More information about Shared Objects can be found HERE

» Read more…

AS3 & Facebook: Uploading Webcam Photos

Facebook.. Could you live without facebook nowadays? Sometimes I’m happy when I don’t get anything on facebook, just to have a bit of peace and quiet of it.

Well, nevertheless, I’m writing this article because I’ve seen this post on the Adobe Forums by dmennenoh where he asked for the possibilities of using the Facebook AS3 API without Flex or AIR and the possibility of uploading pictures to Facebook.

Short answer: yes, it’s possible but…. it’s not a very well documented process of achieving it.

Before we start, I would like to recommend you a few tutorials on the Adobe Developer Connection, Jeanette Stallons wrote a whole series about the usage of the Facebook AS3 API, and it’s an absolute must read if you want to start developing flash applications or websites using facebook data and connectivity. First I recommend you starting with the Quick Start to get a feeling for it, after that move on to Part 1: Build and Test Locally, Part 2: Deploy on Facebook and Part 3: Deploy using Facebook Connect » Read more…

AS3: QuickHint: Convert BitmapData to ByteArray

While writing my next post, about Facebook and Flash development, I noticed that there is something in my post drafts that I haven’t published yet, so I changed it a bit and here you go.

On one of my last posts taking a screenshot with AS3 I’ve explained how to create a snapshot by using the Bitmap and the BitmapData classes.

Sometimes you need to convert the bitmaps to bytearrays to send them to a server-side script, save it directly to an AIR database or send it with AMF.

Something that is still a bit unclear, is how to convert the freshly created BitmapData to a ByteArray, there are no methods available in the default packages that come along with Flash Builder or Flash Professional, there are a few other ways, but they are in my opinion not really the best way on doing it. After all we all like to write short, smart clean code right?
» Read more…

AS3: Drawing dynamic anchor lines

Based on this post on the Adobe’s AS3 forum, I thought  I would create a simple class that takes care of the requested task.

The whole idea is basically to draw a line dynamically between 2 displayobjects, on top of it when one object is being dragged the line is redrawn along based on the second sprite position.

There is  a lot more that can be done, like avoiding other sprites, drawing multiple lines attached to multiple buttons and so on. but I think this class can give you a good head start for future implementations.

First of all there are a few things that we need to consider before starting, let’s take a look at the first few tasks

1. Every Sprite needs to know to which button it has been linked to. You need to store this somewhere.
2. Every Sprite needs a specific anchor point that is used as endpoint of the drawn line.
3. As soon the drag method is started we need to know where  the one end of the line (from the sprite that is not being dragged by the mouse) is positioned at. This is something that we really need to do once on a startDrag method.

Here’s what I’ve done: » Read more…

AS3: Take a screenshot of any Displayobject


Ok you want to take a snapshot of a movieclip, sprite, displayobject, shape or just simply the whole stage?
That is a simple task to accomplish in AS3. Below a reusable function I wrote while working for one of our clients:

private function getBitmapData( target:DisplayObject ) : BitmapData
{
if ( bd )
{
bd = null;
}
//target.width and target.height can also be replaced with a fixed number.
var bd : BitmapData = new BitmapData( target.width, target.height );
bd.draw( target );
return bd;
}

» Read more…