Archive for October, 2007

AS3 – Calculating true Frames Per Second

Monday, October 22nd, 2007

I strongly recommend you look at this new version of this class.

When coding for Flash based games it’s important to know how fast your project is running [even if you're not coding games it's good to know whether or not your code is slogging down the player]. Adobe’s Stage class provides a parameter called “frameRate,” which allows you to dynamically get and set the player’s frame rate. Well, not exactly. It allows you to get and set the player’s TARGET frame rate, the fact is if the player can’t perform your code at that frame rate, it won’t! So how can you find out what the real frame rate is?

I wrote a simple class for this task to use in my project development. It’s super simple. My class, fpsBox, adds an eventListener to the stage [if you pass the stage as an argument when instantiating the class] or to itself, that triggers every frame and increments a value. A timer is started simultaneously with a repeat cycle of one second. Every second the number of counts counted each frame are displayed in the fpsBox and then averaged in an array of instantaneous values. Don’t worry, the array never gets past two values, the current average and the instantaneous FPS. Here’s some usage:

import com.efnx.fps.fpsBox;

var fps:fpsBox = new fpsBox(stage);
     addChild(fps);

That’s it!

What it looks like:

Link to SWF

Source Code
fpsBox Class Source Code

AS3 Animation class to replace MovieClip

Monday, October 15th, 2007

Okay, let me start by saying that I HATE importing resources into fla’s. When I first learned Flash [AS2] it wasn’t an issue. Now with AS3 and the necessity of classes my coding style has changed and I really, really like keeping things as separated into classes as possible.

The Problem With MovieClips
When attempting to program button classes I ran into this wall: Flash’s MovieClip is the only appropriate class object to use for animation. It’s great for placing images in your timeline to form moving pictures and placing Actionscript on certain frames makes directing the clip around really easy. But what about when you have to make MovieClips dynamically and add code to certain frames inside that clip, without using the GUI? MovieClip doesn’t support dynamically adding frames, or functions or Bitmaps to certain frames. This sucks. Why Adobe did this I do not know – so I wrote a class that DOES WHAT I WANT [and maybe you too].

Usage
Animation takes three parameters, which are all optional: numFrames:int = 1 width:int = 0, and height:int = 0. These params just initiate the object.

/*
*     blog.efnx.com:Animation Example
*/


import com.efnx.utils.Animation;     //import the class

var character:Animation = new Animation();     //object sets numFrames, width and height to 1, 0 and 0

After making the object you have to append the pictures to certain frames using the appendBitmapData function:

character.appendBitmapData(theFrame:int, bitmapData:BitmapData);

What this does is take the BitmapData and attach a function to the given frame that replaces the Animation’s default BitmapData with yours upon execution of that frame.
Doing this achieves the same effect as dragging a bitmap onto the stage in a MovieClip keyframe.

I wrote the class keeping in mind that someone may want to use it not for animation but sequential code execution, so frames can be created and functions attached to each frame without any graphics associated. The function used to add [or append] other functions to a certain frame is

     appendFrameScript(theFrame:int, theFunction:Function, [testing:Boolean = false]);

theFrame is the frame you’d like to add a function to, theFunction is the function to be added and testing tells the class whether or not to print debug info.

After adding frames and bitmaps and all that Animation can be controlled using familiar MovieClip methods like gotoAndPlay(), gotoAndStop(), go(), stop() and properties like currentFrame and totalFrames, as well as some others – bmd [the current frames BitmapData], bmdArray [an array of all frames BitmapData] and cool functions like customFps(fps:int) which set a custom playback rate denoted in number of frames per second [fps:int] and accelerateFps(desiredFps:int, numberFrames:int) which accelerates the playback delay from the current FPS to the desired FPS in numberFrames frames. accelerateFps is still a little buggy, feel free to nice it up!

Last Words
Animation doesn’t automatically loop your animation [since I usually don't need it to], so to really mimic a MovieClip you’d have to write a function incorporating gotoAndPlay(1) and then attach the function to the frame you’d like to loop your clip at:

var character:Animation = new Animation();                   //initiate object
     character.appendBitmapData(1, firstBitmapData);       //add BitmapData
     character.appendBitmapData(2, secondBitmapData);
     character.appendFrameScript(2, repeat);                   //append repeat function

function repeat():void                //repeat function
{
     character.gotoAndPlay(1);
}

Hope this class helps some of you out. Feel free to let me know what needs improving and comment if you like it!

Source
Animation Class Source Code

JPEGtoAS3 Image Converter

Saturday, October 13th, 2007

So here’s one of my first projects. It’s called JPEGtoAS3 Converter and it takes an image [JPEG, GIF, PNG or SWF] from the web [specified by an URL] and turns it into a series of BitmapData.setPixel() statements so you can place it in your AS3 class as a function. I made this so I could use graphics in my GUI classes without having to load them from external files. Instead I just place them in a function and create a new bitmap, then draw the pixels to the bitmap using the function.


JPEGtoAS3 Image Converter
[Right click and choose "Save As"]

So, I understand that the real utility of this program is marginal, but it’s still an interesting way to support graphics in classes without externally loading them, and without having to import resources to your .fla file.

Birth Of A Code Blog

Friday, October 12th, 2007

Here it is. My first code blog. I’ve been making little AS3 projects for a couple months and have had no place to put them. Now I have some kind of repository. So if you’re a  programmer, designer, gamer or script kiddie, bookmark my blog – fo fun.


Follow me on GitHub
Follow me on Google+
Follow me on Twitter