var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
a.sort(function(){ return Math.random()-0.5; });
//'a' is now something like
//[9, 0, 2, 3, 4, 1, 8, 5, 6, 7]
Saturday, August 21, 2010
Random array sort in Javascript
There is such an array method called sort, which puts up the elements of an array in alphabetical order, changing it (without copying the whole array). Additionally, it takes also one function parameter for defining the order of elements. If it returns negative number, given elements will be switched, and when positive one, nothing will happen. So to sort an array in random order, it just need to return random numbers in -1 to 1 range.
From the other hand, it is only one week to the NodeKnockout. I'm really looking forward for this!
Thursday, August 19, 2010
Detecting orientation change of mobile devices
Couple of weeks ago I was asked by one of the biggest online games company to adapt several of my games for their new portal designed for mobile devices.
One of the most important thing in this venture was to disable orientation changing of the phone. Simplest way to check if it is in landscape or in portrait mode (in every device, because I know there are functions dedicated for Iphone and maybe some for Android also) is to check if height of the screen is bigger or lower than it's width.
I know it won't works in IE, but almost all of the mobile web browsers are based on WebKit.
One of the most important thing in this venture was to disable orientation changing of the phone. Simplest way to check if it is in landscape or in portrait mode (in every device, because I know there are functions dedicated for Iphone and maybe some for Android also) is to check if height of the screen is bigger or lower than it's width.
It's not possible to guess in which mode user enters the page (game in that case) so it's important to check it in the begging, and each time the browser's window resize.
function checkOrientation() {
if (window.innerHeight > window.innerWidth) {
//portrait mode
} else if(window.innerHeight< window.innerWidth) { //landscape mode
}
}
Try this link from your phone to test it, or just open it in desktop browser and change window size: LANDSCAPE VS PORTRAIT
checkOrientation();
window.onresize = function() { //your code
checkOrientation();
}
I know it won't works in IE, but almost all of the mobile web browsers are based on WebKit.
Wednesday, August 18, 2010
Recursion of anonymous functions
When I declare anonymous function, like
name of the function is not added to any scope, either local or global, because there is no name at all. When I want to call the function again, for example in timer functions like setTimeout(), I simply call arguments.callee inside it
(function(){
//do something...
})();
According to Mozilla Developers Center:
(function(){
//do something...
setTimeout(arguments.callee, 1000);
})();
arguments.callee allows anonymous functions to refer to themselves, which is necessary for recursive anonymous functions.
Tuesday, August 3, 2010
"Stairs to heaven in 6,22KB" competition!
My best try (of course it doesn't count):
Subscribe to:
Posts (Atom)