Friday, June 22, 2007

Vivid random colors

If you want more control over how to create random colors, you need to steer away from the RGB space and go into the more human-friendly HSB color space, which is Hue/Saturation/Brightness.

Unfortunately flash doesn't natively support this but you can convert between the color spaces using some help functions like the following which were found here:

function hsv2rgb(hue, sat, val) {
var red, grn, blu, i, f, p, q, t;
hue%=360;
if(val==0) {return({r:0, g:0, v:0});}
sat/=100;
val/=100;
hue/=60;
i = Math.floor(hue);
f = hue-i;
p = val*(1-sat);
q = val*(1-(sat*f));
t = val*(1-(sat*(1-f)));
if (i==0) {red=val; grn=t; blu=p;}
else if (i==1) {red=q; grn=val; blu=p;}
else if (i==2) {red=p; grn=val; blu=t;}
else if (i==3) {red=p; grn=q; blu=val;}
else if (i==4) {red=t; grn=p; blu=val;}
else if (i==5) {red=val; grn=p; blu=q;}
red = Math.floor(red*255);
grn = Math.floor(grn*255);
blu = Math.floor(blu*255);
return ({r:red, g:grn, b:blu});
}
//
function rgb2hsv(red, grn, blu) {
var x, val, f, i, hue, sat, val;
red/=255;
grn/=255;
blu/=255;
x = Math.min(Math.min(red, grn), blu);
val = Math.max(Math.max(red, grn), blu);
if (x==val){
return({h:undefined, s:0, v:val*100});
}
f = (red == x) ? grn-blu : ((grn == x) ? blu-red : red-grn);
i = (red == x) ? 3 : ((grn == x) ? 5 : 1);
hue = Math.floor((i-f/(val-x))*60)%360;
sat = Math.floor(((val-x)/val)*100);
val = Math.floor(val*100);
return({h:hue, s:sat, v:val});
}
So to create vivid random colors you just randomize the Hue, and set a high Saturation and Brightness. If you just want random shades of a color, you set the Hue and Saturation and just randomize the Brightness.

Tuesday, June 19, 2007

Random seed not settable in Flash Pseudo-Random Number Generator

I'm still polishing up the puzzle game I'm working on. I was in a debugging session and I wanted to set the random seed for the PRNG. And surprise!!! There is no method to set up the random seed for Math.random().

However I came across this which provides a class for a PRNG. Pretty useful ;)

Tuesday, June 12, 2007

Dynamic text problem

I wanted to markup some text in a dynamic text field using the .htmltext property . But the bold characters were not showing. I found the answer to the problem here.

I just needed to create an off-stage dynamic text whose only purpose is to embed the bold font. If I wanted italic as well, i would have to do another dynamic text which embeds the italic font.

Now if only flash warned me that there are missing font definitions, argh!

Array instantiation for actionscript classes

If you want to avoid some nightmares, don't instantiate the arrays in the variable definition inside a class.
i.e. DON'T do the following

class myClass {

var my_array:Array = new Array()

function myClass()
{
}

....
}


Coz this would end up with a behaviour similar to having the my_array defined as static. What I think that it is happening, is that it won't re-initialise the variables when you instantiate a class but will just give you a reference to the array. Thus each instance you create will have a reference to the same array. How dumb!

So the solution is ofcourse to move the array instantiation in the constructor, like so:

class myClass {

var my_array:Array;

function myClass()
{
my_array = new Array();
}

....
}

Monday, June 11, 2007

Dictionary service and getTextExtent

I'm still working on the word puzzle game so far.
I got this idea this morning, that when you manage to connect a word it would access an online dictionary service and give you a definition. Basically I was testing the game and I was forming words that I didn't ever hear before. So you can actually learn something by playing this game :)

I found this free dictionary service
http://services.aonaware.com/DictService/
I'm currently using it to request word definitions and then place them in a speech bubble.

I also worked on scrolling the speech bubble text if the definition didn't fit. I used the TextFormat.getTextExtent() method, even though it is errouneously marked as deprecated for Flash8.

Friday, June 8, 2007

Word list in Flash word game

I'm working on a word puzzle game for a client and I had the challenge to find if a word was created on a game board and had to access 28,000 words (taken from a word list found at http://wordlist.sourceforge.net/ which are used for spell checking, word games, etc)
Originally there were over 80,000 words but I trimmed off the long words which cannot be created on the board.

To overcome the slow speed of flash accessing a huge array, I split up the word list in 26 arrays (one for each letter). This was not enough. You could tell that the CPU was doing something since the animation stop until he checks the board. So I re-split each array into another 26 arrays. Flash doesn't have red-black trees (or is there??) so I had to improvise ;) This was the simplest and now you won't notice it is checking the board for 28,000 words :D.

Wednesday, June 6, 2007

Not much progress this week

Not much progress on the prototype this week. I'm currently busy doing a flash puzzle game for a client and also getting updated on all the changes (and problems/bugs) with AS3.