Showing posts with label actionscript. Show all posts
Showing posts with label actionscript. Show all posts

Thursday, July 30, 2009

Code Optimization in Actionscript

3D graphics in Flash are usually handled by the CPU almost no assistance of any fancy 3D acceleration GPU. Thus, framerate is a very important concern.

I found a very useful guide on Code Optimization in Actionscript. For exampled, it states:

"Denormal numbers are floating point numbers that are really small. When such a number occurs the CPU switches into denormal mode and calculations become very slow.

Example


In this example the framerate will jump from approximately 40fps to 1fps. The reason are denormal numbers.


private var _denormals: Vector.;

private function init(): void
{
_denormals = new Vector.( 0xfffff, true );

var n: int = 0xfffff;

while( --n > -1 )
_denormals[ n ] = 1e-256;//very small number!

EnterFrameProvider.connect( enterFrame );
}

private function enterFrame(): void
{
var n: int = 0xfffff;

while( --n > -1 )
_denormals[ n ] *= 0.5;//do some calculations with very small numbers
}


ActionScript Solution

There are a couple of solutions to deal with denormal numbers. Some try to avoid them by adding always a very small value or by adding some white noise. In ActionScript the best and most simple solution is by adding and subtracting an anti-denormal constant.
[edit] Solution

Example

This solution is easy to implement in ActionScript and will result in real zero values.


_denormals[ n ] = _denormals[ n ] * 0.5 + 1e-18 - 1e-18;//avoiding denormals


This is the same example with the fix for denormal numbers. The framerate will now stay at approximately 40fps."

Saturday, July 25, 2009

Using Actionscript for 3D

Recently, I have toying around with 3D in flash. There are several frameworks to produce 3D graphics (e.g. Papervision 3D, Sandy, Away3D, Yogurt3D, FIVe3D, etc) which you can use together with other frameworks for physics (Wow-Engine, JigLibFlash, box2dflash, APE), object distortion (AS3Dmod), augmented reality (FlarToolKit), etc.

When working, I quickly realized that using primitive shapes to build a 3D scene with ActionScript was only useful to a certain point. To supplement this, some allow the importing of COLLADA models, which is a standard that represents models with an XML file and an image used for texture mapping.

When searching for some models to use in my projects, I stumbled on Google's 3D Warehouse (a place that stores 3D models that people created) and SketchUp (a tool to create 3D models).


[Source: papervision2.com]

Hope you found that interesting.

Monday, June 29, 2009

Using FaceBook API from ActionScript

About two month ago, FaceBook announced it's Open Stream API (also known as Facebook Connect) that allowed applications to interact with FaceBook and replicate most of it features.

To demonstrate this, they got their partner, Adobe to create Facebook for Adobe AIR, which is an application that runs on your desktop. You log in with your Facebook account and can then read your News Feed and publish content to your Wall and friends' News Feed.

The source code for this application is publicly available and I recently took a look at it.
(UPDATE: there is now a project on Google Code for it too)

Of particular interest is how it communicate with FaceBook:


var request:URLRequest = new URLRequest("http://api.facebook.com/restserver.php");
request.contentType = "application/x-www-form-urlencoded";
request.method = URLRequestMethod.GET;
request.data = urlArgs; // a flash.net.URLVariables object


But all this is nicely encapsulated for you in the "fb" package.

Some uses would be to upload screenshots and post achievements in Flash games directly to your FaceBook wall. Or it can be used to allow easy multiplayer game matchmaking with your friends.


The source code from this application also show some advanced ways to display HTML formatted text using ActionScript (and Flex). Thus, its a rather nice example.


Also, here is a tutorial (from Adobe) that walks beginners like me on how to register a FaceBook application and get its API key, however it uses Flex instead of Flash.