rangeToPercent and percentToRange

Two very useful functions / formulas to convert a percentage number to any range values or any number to a percent range.

function rangeToPercent(number, min, max){
   return ((number - min) / (max - min));
}
 
function percentToRange(percent, min, max){
   return ((max - min) * percent + min);
}

Enjoy!

Starling and Feathers Full Screen Button

Today I tried to make a full screen button for my Christmas Day Countdown online application. With that application I’m using Starling with Feathers components. If you listen for Event.TRIGGERED on a Feathers button and then change the displayState, you will get a Security error:

SecurityError: Error #2152: Full screen mode is not allowed.

Flash Player is expecting a MouseEvent when changing the displayState, it will not work with a Touch event. I looked for a workaround and I found this forum topic. After I made some improvements, I finally had a working full screen button triggered from Starling. The code below works like a charm:

Starling.current.nativeStage.addEventListener(MouseEvent.MOUSE_DOWN, toggleFullScreen);
 
private function toggleFullScreen(event:MouseEvent):void
{
	if(_fullScrenButton.stage != null)
	{
		if(_fullScrenButton.stage.hitTest(new Point(event.stageX, event.stageY)) == _fullScrenButton)
		{
			if(Starling.current.nativeStage.displayState == StageDisplayState.FULL_SCREEN)
			{
				Starling.current.nativeStage.displayState = StageDisplayState.NORMAL;
			}
			else
			{
				Starling.current.nativeStage.displayState = StageDisplayState.FULL_SCREEN;
			}
		}
	}
}
Color Value To Hexadecimal

A simple utility class that will help you convert a color value (from numbers) to hexadecimal color value.

Basically you can convert the color value to hex string with colorNumber.toString(16);
Now, if you have this shade of blue “13311”, for example, it will be converted to “33ff”.

You can fix that with while(hexColor.length < 6) and add “0” to in front of the hex color output.

Read more
Quick Tip: Replace Breakline (\n) with comma

Today I have a very quick tip but useful. I’ve wasted a few minutes of my life trying to find an answer for this because I’m not an expert when it comes to regular expressions.

This code comes in handy if you’re trying to remove \n (a break line) from a string and you want to replace it with something else (line a comma, as shown in the example below) you can use the following code:

string.replace(/[\r\n]+/g, ",");
Numbers to letters with ActionScript 3

Numbers-to-letters Today I’ve been working on a project where I needed to convert numbers to letters.

I’ve made a nice utility class for this simple task.
Not sure if it’s the best way but it works perfectly fine.

Basically I have an array where the items need to be in alphabetical order instead of numbering them with 1,2,3,4,etc.

Read more
Quick Tip: Test to see if an XML attribute is present

Today I received a question about how to check and see if an attribute is present into a XML node.

I thought it wold be a good idea if I post the answer here in case someone else needs to do the same.

This is the ActionScript code:

if(node.@attributeName in node)
{
    //the attribute is here
}

As you can see, the code is very simple, “node” is your actual XML node and “attributeName” is exactly what it says, the name of your attribute inside the XML node (the one that you are checking to see if it is there).