Configuring WAMP server to work on Windows 10 with Netbeans

I’ve been doing more PHP lately and ran accross a problem that I needed to debug locally. So I downloaded WAMP and of course it didn’t work the first try, so I downloaded an update for it that fixed the launching error that it was having. It would launch, but only 1 of the 2 services was running. After doing some research I found that Apache wasn’t starting. After some help from Google I found that I needed to change the port from 80 to 8080. That worked, but then after all of the configurations in Netbeans I couldn’t get the debugger to work. After a long time and some research and trial and error I found that I needed to make the following changes in the php.ini:

zend_extension =”c:/wamp/bin/php/php5.6.25/zend_ext/php_xdebug-2.4.1-5.6-vc11.dll”
xdebug.remote_autostart=on
xdebug.remote_enable=on
xdebug.remote_handler=”dbgp”
xdebug.remote_host=”localhost”
;xdebug.remote_connect_back=1
xdebug.remote_port=9001
xdebug.remote_mode=req
xdebug.idekey=”netbeans-xdebug”

After doing that it worked just fine. I’m putting this in mainly for my own reference, but maybe it can help someone else if they encounter similar problems.

Get Spark Image width or height

So one of the most annoying things about working with display items in the Flex framework is the difference in how things are rendered. This means that after you have created an object in AS3 code you can’t get the properties of that object until after a certain event in the creation process. Generally you need to listen to FlexEvent.CREATION_COMPLETE. After that fires you can usually get width and height etc. the Spark image is a little different. The best way that I’ve found is to listen to Event.COMPLETE and then get sourceWidth or sourceHeight instead of width or height. This one drove me nuts for longer than it should have so hopefully if you have this problem you can find this post before it drives you nuts.

TypeError: Error #1007: Instantiation attempted on a non-constructor

So I know that many people are jumping ship when it comes to Flash and Flex when it comes to web applications but if you are one of the ones like me that is still doing this type of development you might run in to this sometime. The app that I am working on is a Flex app that uses the 4.6 sdk. The is a central module that loads other modules. The problem I ran into was on one of the modules that was using an AdvancedDataGrid. I kept getting the Error #1007: Instantiation attempted on a non-constructor. The solution ending up being something very simple. The main application just needs to have an instance of the AdvancedDataGrid. I simply added AdvancedDataGrid; that along with the import and you should be good to go.

Remove Duplicates from an Array AS3

I recently needed function that would remove duplicate values from an array. After looking around online and modifying what I found to fit my needs I came up with the following:

var arr:Array = ["a", "b", "c", "a", "d", "a", "a"];

removeDuplicates(arr);

trace(arr);

function removeDuplicates(target:Array):void
{
	for(var i:uint=target.length; i>0; i--)
	{
		if(target.indexOf(target[i-1]) != i-1)
		{
			target.splice(i-1, 1);
		}
	}
}

If you are searching for a way to do this, I hope this helps!

Random Vector and Random Array Functions

I’ve been working on a Blackjack game and have needed to do a lot of shuffling, cards that is. I’ve seen quite a few shuffling functions over the years. Here is the one I like, partly because I wrote it. Well okay I took what I liked in the others and modified them to come up with these:

public static function randomArray(src:Array):void
{
    var len:uint = src.length;
    for(var i:int=len-1; i>=0; i--)
    {
        var randIndex:uint = randomUint(0, len-1);
        var temp:* = src[randIndex];
        src[randIndex] = src[i];
        src[i] = temp;
    }
}

Vector version looks like this:

public static function randomVector(src:*):void
{
    var len:uint = src.length;
    for(var i:int=len-1; i>=0; i--)
    {
        var randIndex:uint = randomUint(0, len-1);
        var temp:* = src[randIndex];
        src[randIndex] = src[i];
        src[i] = temp;
    }
}

Here is a sample of how the vector version is used:

Randomize.randomVector(cards as Vector.<Card>);

Here is the randomUint function that it uses:

public static function randomUint(min:uint, max:uint):uint
{
    return Math.random()*((max+1)-min)+min;
}

I like to call these functions a random number of times so that the deck of cards is nice and shuffled. Enjoy!

How to embed fonts using actionscript 3

This was causing my to bang my head for a little while. There are different ways depending on  whether you are using Flash or Flash Builder (previously Flex Builder). If you are using Flash you can go here for a good example/tutorial: http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/. Basically you add a font to the library by clicking the pop-up menu (in the upper-right corner of the Library panel), export it for actionscript and then give it a class name. After that your code looks something like this:

var myFont:Font = new Font1();
var myFormat:TextFormat = new TextFormat();
myFormat.font = myFont.fontName;

Embedding fonts in AS3 seems to be a big problem for many people out there especially if you are using Flash Builder/Flex Builder. I know it was driving me crazy! I was following the examples, but still seemed to keep getting the same error: “An Embed variable must not have an existing value.”. Okay maybe it’s just me but that one was a little cryptic. Finally after a long time, but before having to do a Google search for straight jackets I figured out what I was missing. In all of the examples the embed line was right above a Class variable, but mine was not, so I added one and  cha ching it worked. Here is a sample:

[Embed(source="assets/MONACO.TTF", fontName="monaco",
mimeType="application/x-font-truetype")]
private var MonacoFontEmbed:Class;

It may just be me but this was not obvious I must confess. You don’t use that variable at least I didn’t, you simple set the .font property to the fontName value. It looks something like this:

myFormat.font = "monaco";

monaco is what I named it. You can name that whatever you want along with the variable. I named it MonacoFontEmbed, but you can name it whatever. I hoped this helped. If you have any questions feel free to post them. Embedding fonts is very useful especially if you are planning to manipulate a text field. Manipulations like rotation will just cause your text field to disappear if created dynamically and without embedding the font.

W3C Page Validation Issue

It’s good practice to always make sure that each page on your site validates to its proper doctype. Recently I noticed that some pages on a site I did that use to validate no longer did. It was giving me an error saying that it didn’t know how to decode Content-Encoding ‘none’. This was saying that I wasn’t declaring the encoding at the top of my page. The thing is I was. Here is what the problem was. I am using php and have an external php page that contains functions on it that are used by the page and I was including that page at the top of the document. The problem with this was that it was above the doctype declaration. All I had to do was move it down one line and that fixed it. I’m not sure if something has changed with the validation because it use to validate, but if you run into this problem just move it down one line. Here is an example:

Before:
<?php include_once(“my-file.php”);?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

After:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<?php include_once(“my-file.php”);?>

PS
I use a cool plugin for Firefox called Web Developerthat has a built-in tool that will validate your page straight from the W3C Validator. If you are not currently using this I suggest checking it out!

How to format a number as money

Recently I was working on a project where I had to take a number that represented a monetary value and display it as money. For example say I had a numeric variable that contained the value of 10000 and I needed it to display like money with the commas and dollar sign like this $10,000. So in other words I needed the string representation. That is what this simple function that I am posting does. I have written it here in Flash ActionScript 3 as a static function of a class, but you could adapt it however you need to. Here is the code:

public class Formatter
{
    public static function formatMoney(val:int):String
    {
        var isNegative:Boolean = false;
        var moneyVal:String = val.toString();
        var len:uint;
        if(val < 0)
        {
             isNegative = true;
             moneyVal = moneyVal.replace(/-/, "");
        }
        len = moneyVal.length;
        for(var i:int=len; i>0; i-=3)
        {
            if(i == len)
            {
                continue;
            }
            var firstHalf:String = moneyVal.substring(0, i);
            var lastHalf:String = moneyVal.substring(i);
            moneyVal = firstHalf+","+lastHalf;
        }
        if(isNegative)
        {
            return"$-"+moneyVal;
        }
        return "$"+moneyVal;
    }
}