Archive

Archive for the ‘programming’ Category

PaperVision3D one to one pixel representation

February 24th, 2009 admin 3 comments

It was hard for me to find the correct camera settings in papervision to achieve a one to one pixel representation of my objects at z = 0, so here they are:

camera.focus = 10;
camera.zoom = 100;

Voilá! Now move your DisplayObject3D to x, y and z = 0 and you should see it at its actual pixel size.

Categories: programming Tags:

Microphone activityLevel in Flash

January 14th, 2009 admin 8 comments

I wanted to analyse the frequencies of the microphone input in Flash, but guess what? You can’t!

With streaming sound it’s no problem, as this tutorial shows. But surprise: the microphone input is not accessible so you can’t respond to specific frequencies in you live sound input. Crap.

Anyway, after discovering this I set out to simply use the volume of the input, which is represented in the activityLevel property of the Microphone class. When activity is registered (you can set a threshold volume above which it does so), the ActivityEvent.ACTIVITY is dispatched, so you can respond to that.

Get ready for my next surprise: I only see the event being dispatched when my security settings panel is open, and then only when the microphone tab is active! Wtf is going on here?!

After battling with this, and trying out different possible solutions, it turns out that to make it work, you need to have set the loopback setting to true (microphone.setLoopBack(true)). This means that the microphone input will be played back over your speakers.

Needless to say you won’t always want this to happen. In fact, you probably néver want this to happen (think of the feedback!), but apparently you have to in order to be able to read the input volume at all.

Luckily the solution is fairly straightforward: set the soundTransform property of the microphone to a SoundTransform instance with volume zero and presto: no mic sound playing back through your speakers, but an ActivityEvent.ACTIVITY event triggering nonetheless.

Categories: programming Tags:

setTimeOut crashes Flash player

December 2nd, 2008 admin No comments

I just spent the best part of the morning fixing a bug in a website that made my browser crash when I was trying to play a video in Flash using the FLVPlayback component.

What turned out to be the problem? Well, it was the fact that we used the built-in setTimeOut method to keep checking the buffering progress of the video. It would crash Flash player without exception. I removed the call to this method and replaced it by an enter frame listener, and now it works like a charm. No more crashing.

I haven’t tested it thoroughly yet, but it seemed to appear after I had upgraded to Flash CS4, making Flash Player 10 a suspicious candidate. So my suggestion is: avoid this utility method where possible!

Categories: programming Tags:

Extremely safe singleton classes

November 10th, 2008 admin 1 comment

I’ve found a solution to creating singleton classes that make it absolutely impossible for any class outside it to instantiate it directly.

The idea is that you create an inline class (in this case called ‘SingletonEnforcer’) and require an instance of that inline class as a parameter in the constructor of the singleton class. No class outside of it can instantiate SingletonEnforcer, and therefore no class outside of it can directly instantiate the singleton class itself!

Here’s the example code:

package
{

public class SingletonClass
{
private static var instance : SingletonClass;

public static function getInstance() : SingletonClass
{
if (!instance)
{
instance = new SingletonClass(new SingletonEnforcer());
}
return instance;
}

public function SingletonClass(enforcer : SingletonEnforcer)
{
if (!enforcer)
{
throw new Error(”SingletonClass must be instantiated through SingletonClass.getInstance()”);
}
}

}
}

class SingletonEnforcer {}

I’ve found this example on PixelBreaker’s blog. Check it out.

Categories: programming Tags:

Executing HTML links in Flex

October 27th, 2008 admin 1 comment

In the project I’m currently working on, I found that HTML links in textfields weren’t properly executing, although the text seemed to be rendered properly. My mouse cursor even changed to a hand cursor on rollover of the link within the text, but I could only actually ópen the link through the context menu that popped up after a control-click (or right click for the PC user).
After some searching, I found the simple answer to my problem on the Flex Examples blog: in order for the HTML link to be properly executed, the ’selectable’ property of the containing TextField must be set to ‘true’! If the text is not selectable, your HTML link won’t automatically be opened on click. Way to go Adobe!

Categories: Flex, programming Tags:

Removing ugly yellow focusRect in Flex

October 10th, 2008 admin 4 comments

Right now I’m working on a project in which Flex integrates Flash components in its application and we encountered the infamous yellow focus borders when hitting the tab key from inside the application.

It wasn’t as easy as I thought it would be to Google the solution to prevent the ugly yellow rectangles from being displayed. I’ve found the solution in the Flex help though, so I’ll try to improve the search results on Google by providing the solution here.

When you look at the focusRect property description of flash.display.InteractiveObject, it says: “Specifies whether this object displays a focus rectangle. A value of null indicates that this object obeys the stageFocusRect property set on the Stage.”
And there’s your solution! In your Flex application, create a method that is called on creationComplete and in this method add the following code:

stage.stageFocusRect = null;

Now all focus rectangles are globally disabled in your application. It’s as simple as that.

Categories: Flex, programming Tags:

Particle experiments

September 9th, 2008 admin 3 comments

Here’s some small experiments I did in ActionScript 3 with particles in combination with perlin noise determining how they move.

In all cases it’s a bunch of particles placed in a circle, each one having its position linked to a randomly chosen pixel in a scrolling perlin noise filled bitmap. The third example has only a few particles and interconnects them with a curve, creating a moving type of amorphous blob.

pulsating iris

pulsating fountain

globule

Edit: I’ve added a small motion blur effect to the globule to make it a little bit more interesting, visually.

Categories: programming Tags:

ComboBox text formatting in AS3

April 15th, 2008 admin 38 comments

After struggling with it for a while, I’ve finally found the way to set embedFonts and a specific textFormat for all text in a Flash CS3 ComboBox component.

First of all, it is wise to create a subclass of fl.controls.ComboBox to represent your custom comboBox. In this class you can then set some styles in the constructor as follows:

package com.rockabit.utils.as3
{
import fl.controls.ComboBox;
import fl.controls.List;

import flash.text.TextFormat;

/**
* Customized ComboBox component. Graphic changes have been made inside the component itself,
* in the library, changes in textual display are made here.
*/
public class CustomComboBox extends ComboBox
{
private var textFormat:TextFormat;

public function CustomComboBox()
{
super();

textFormat = new TextFormat(”Arial”, 10, 0xffffff);

//textField is the top text, in the closed combobox
textField.setStyle(”embedFonts”, true);
textField.setStyle(”textFormat”, textFormat);

//dropdown is the list that shows when you open the combobox
dropdown.setStyle(”cellRenderer”, CustomCellRenderer);
}

override protected function drawLayout():void
{
super.drawLayout();
textField.y = 0;
}
}
}

So in the constructor, I create a TextFormat instance and assign that to the textField variable. I then set the cellRenderer style of the dropdown list, which is a class that describes how each item in the list is formatted. For that I use another custom class, which extends fl.controls.listClasses.CellRenderer:

package com.rockabit.utils.as3
{
import fl.controls.listClasses.CellRenderer;

import flash.text.TextFormat;

/**
* Custom cellRenderer for comboBox list items.
*/
public class CustomCellRenderer extends CellRenderer
{

public function CustomCellRenderer()
{
super();
this.setStyle(”embedFonts”, true);
this.setStyle(”textFormat”, new TextFormat(”Arial”, 10, 0×000000));
}

}

}

All this class does is extend the default CellRenderer class and set some styles in the constructor.

Using these two simple classes, you can easily make sure that all your fonts are embedded in the comboBox component, and give them custom text formatting.

I’ve made a small example .fla file. You’ll notice that I added the Tahoma font to the library with a linkage class, which I instantiate in the TextFormat constructor parameter to get the correct fontName. Because I set the library font to bitmap text andset embedFonts to true in code, you get a nice crisp font display in your combobox.

Edit: I have now also added functionality to the CustomComboBox class to set vertical positioning of the main textfield. Right now this is hardcoded, but that could be changed to listen to a custom style.

Download source:
http://www.rockabit.com/downloads/as3/combobox_example.zip

Categories: programming Tags:

Navigatable ArrayCollection for Flex

March 18th, 2008 admin 1 comment

I’ve written a NavigatableList class to provide the already handy ArrayCollection class with a simple cursor-like navigation option. You can call list.navigateTo() and provide for instance list.next or list.previous. You also specify the class to which items should belong and you can search for an item with list.findItem(property, value). And finally, you can clone it with list.clone().

Download it here

EDIT: the clone() method didn’t work correctly for some reason. I’ve removed it for now.
EDIT: added an equals() method which compares this list to another list. You  must specify the  name of a unique property which can be used to check if two items are the same.

Categories: Flex, programming Tags:

AS 3.0 layout manager

February 25th, 2008 admin 5 comments

I’ve created a custom layout manager in AS 3.0 which provides some of the automatic scaling and positioning that Flex provides.

You can register objects and specify a size to maintain relative to the stage size, alignment, padding and set a function that is called when the screen is resized.

This is a first version and it may (and most likely will) change in the future.

Download: LayoutManager.as and LayoutTarget.as

Categories: programming Tags: