Wednesday, January 23, 2008

I have made a PlayheadNotifier class that I use to communicate with Flash's timeline.
Sometimes you want to execute a function only when a certain frame of animation is reached.

However, there is one caution -- what happen if the animation was intercepted and the frame was never reached? Your function will never get executed. If you are setting state information in that function, then your object will be in an incorrect state.

When coding in Flash, you must always be mindful that any code execution path that is tied to your playhead may be intercepted.

Tuesday, January 22, 2008

We all know what Math.floor and Math.ceil do, but do you know the relationship between the two? Or asked another way, how would you implement ceil function using Math.floor (or vice versa)?

Imlementing ceil using Math.floor:-

function ceil(value:Number):Number
{
return -Math.floor(-value);
}

What's the fastest way to test if a number is odd number?

function isOddNumber(value:Number):Number
{
//--> your test here
}


Here is my version: The idea is by checking if the lowest bit of the number is set

function isOddNumber(value:Number):Number
{
return (value & 1 == 1);
}

What's wrong with the following code:

var loop = levelNum * enemiesPerLevel;
if (level > 10) loop *= 1.5;
while (loop--) {
// spawn enemy
}


The code above was used in a game engine that I had to work with recently. It's used to spawn the number of enemies depending on the level. If the level has gone up to 10, the intention is to up the enemies count by 50%. However, there is a serious flaw to the code, where is it?

The problem is in the loop *= 1.5; The fractional multiplication will sometimes produce a floating number in the while condition. So it's possible for the condition to still pass when the loop count to skip past zero to negative value, which still passes the test!

Saturday, January 12, 2008

How to find occupied port number

I was installing Apache 2.2.6 on my machine today and when I tried to run Apache, it says it has problem binding to the address 0.0.0.0:80. I have run into this issue in the past so this doesn't cause as much panic this time around. Basically, you just need to find out what program is using port 80, unblock it and you are set to go.

Here is what I usually do:

1) Run command prompt
2) Type netstat -ano
3) Look for the line that says 0.0.0.0:80 in the Local Address column
4) Look at the PID in the last column. In my case, it says 3076
5) Fire up your task manager, click Processes tab
6) If you don't see the PID column in the listbox, click View->Select Columns and check PID (Process Identifier)
7) Now you should see the PID in the listing. Search for the row that has PID 3076, in my case, the image name that takes up port 80 is skype.
8) Quit skype, re-run Apache (no more bitching about port being taken by someone), relaunch skype. Everything is good to go.